Commit d7f49cca authored by Dosenwerfer's avatar Dosenwerfer

Added existing files

parent bf4bd0e9
%%%-------------------------------------------------------------------
%%% @author js6673s
%%% @doc A game of connect four
%%%
%%% @end
%%% Created : 10. Jan 2020 21:43
%%%-------------------------------------------------------------------
-module(connectfour).
-author("js6673s").
%% API
%%-compile(export_all).
-export([red/0, yellow/0]).
red() ->
register(red, self()),
play(yellow, '[email protected]', r).
yellow() ->
register(yellow, self()),
{red, '[email protected]'} ! {emptyboard(), none},
play(red, '[email protected]', y).
emptyboard() ->
[[o, o, o, o, o, o, o],
[o, o, o, o, o, o, o],
[o, o, o, o, o, o, o],
[o, o, o, o, o, o, o],
[o, o, o, o, o, o, o],
[o, o, o, o, o, o, o]].
% Enters the game loop:
% 1. Receive game state message
% 2. Print board
% 3. Print potential winner
% 4. Else insert a coin
% 5. Check if game has ended now and print winner
% 6. Send game state message with new board and new winner to other player
play(OtherPlayer, OtherNode, Symbol) ->
receive
{Board, r} ->
print(Board),
io:format("Red (X) has won!~n");
{Board, y} ->
print(Board),
io:format("Yellow (O) has won!~n");
{Board, none} ->
print(Board),
{ok, Column} = io:read("Your turn! Enter a column number (followed by .): "),
NewBoard = insert(Board, Column - 1, Symbol),
print(NewBoard),
NewWinner = getwinner(NewBoard),
case NewWinner of
r -> io:format("Red (X) has won!~n");
y -> io:format("Yellow (O) has won!~n");
none -> io:format("Waiting for your opponent's move...~n")
end,
{OtherPlayer, OtherNode} ! {NewBoard, NewWinner}
end,
play(OtherPlayer, OtherNode, Symbol).
% Print board
print(Board) ->
io:format(" 1 2 3 4 5 6 7 ~n"),
printBoard(Board).
printBoard([]) -> [];
printBoard([H | T]) ->
printrow(H),
printBoard(T).
printrow([]) -> io:format("~n");
printrow([H | T]) ->
case H of
r -> io:format("[X]");
y -> io:format("[O]");
o -> io:format("[ ]")
end,
%% io:format("~s ", [H]),
printrow(T).
% Coin insertion
insert(Board, Column, Color) ->
transpose(insertT(transpose(Board), Column, Color)).
insertT([], _, _) -> [];
insertT([H | T], 0, Color) -> [set(H, getstacktop(H, 0), Color) | T]; % Include edited column and tail
insertT([H | T], Column, Color) -> [H | insertT(T, Column - 1, Color)]. % Copy list and find column
getstacktop([], Row) -> Row - 1;
getstacktop([r | _], Row) -> Row - 1; % Return previous cell as next blank cell
getstacktop([y | _], Row) -> Row - 1; % Return previous cell as next blank cell
getstacktop([_ | T], Row) -> getstacktop(T, Row + 1). % Skip blank cells
set([], _, _) -> [];
set([_ | T], 0, Color) -> [Color | T]; % Include edited row and tail
set([H | T], N, Color) -> [H | set(T, N - 1, Color)]. % Copy list and find row
% Check for win condition
getwinner(Board) ->
BoardTL = lists:append(transpose(Board)),
getwinnerR(BoardTL).
getwinnerR(BoardTL) ->
case BoardTL of
[H | T] ->
case [H | T] of
[r, r, r, r | _] -> r; % Row
[r, _, _, _, _, _, _, r, _, _, _, _, _, _, r, _, _, _, _, _, _, r, _, _, _, _, _, _ | _] -> r; % Column
[r, _, _, _, _, _, r, _, _, _, _, _, r, _, _, _, _, _, r, _, _, _, _, _ | _] -> r; % /
[r, _, _, _, _, _, _, _, r, _, _, _, _, _, _, _, r, _, _, _, _, _, _, _, r, _, _, _, _, _, _, _ | _] -> r; % \
[y, y, y, y | _] -> y;
[y, _, _, _, _, _, _, y, _, _, _, _, _, _, y, _, _, _, _, _, _, y, _, _, _, _, _, _ | _] -> y;
[y, _, _, _, _, _, y, _, _, _, _, _, y, _, _, _, _, _, y, _, _, _, _, _ | _] -> y;
[y, _, _, _, _, _, _, _, y, _, _, _, _, _, _, _, y, _, _, _, _, _, _, _, y, _, _, _, _, _, _, _ | _] -> y;
_ -> getwinnerR(T) % Else move on
end;
[] -> none % End of board and no win situation occured
end.
% Source: https://stackoverflow.com/questions/5389254/transposing-a-2-dimensional-matrix-in-erlang
transpose([[] | _]) -> [];
transpose(M) ->
[lists:map(fun hd/1, M) | transpose(lists:map(fun tl/1, M))].
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment