Created
June 20, 2019 02:08
-
-
Save JEG2/9a565a78a7a6a8854d537f47fe440e35 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule TicTacToe do | |
def play do | |
move(" ", " ", " ", " ", " ", " ", " ", " ", " ", "X") | |
end | |
def draw_grid(a1, b1, c1, a2, b2, c2, a3, b3, c3) do | |
IO.puts "\n a b c " | |
IO.puts "1 #{a1} | #{b1} | #{c1} " | |
IO.puts " ---+---+---" | |
IO.puts "2 #{a2} | #{b2} | #{c2} " | |
IO.puts " ---+---+---" | |
IO.puts "3 #{a3} | #{b3} | #{c3} " | |
end | |
def move(a1, b1, c1, a2, b2, c2, a3, b3, c3, player) do | |
draw_grid(a1, b1, c1, a2, b2, c2, a3, b3, c3) | |
move = String.trim(IO.gets("\n#{player} move? ")) | |
make_move(move, a1, b1, c1, a2, b2, c2, a3, b3, c3, player) | |
end | |
def make_move("a1", " ", b1, c1, a2, b2, c2, a3, b3, c3, player) do | |
winner(player, b1, c1, a2, b2, c2, a3, b3, c3, next_player(player)) | |
end | |
def make_move("b1", a1, " ", c1, a2, b2, c2, a3, b3, c3, player) do | |
winner(a1, player, c1, a2, b2, c2, a3, b3, c3, next_player(player)) | |
end | |
def make_move("c1", a1, b1, " ", a2, b2, c2, a3, b3, c3, player) do | |
winner(a1, b1, player, a2, b2, c2, a3, b3, c3, next_player(player)) | |
end | |
def make_move("a2", a1, b1, c1, " ", b2, c2, a3, b3, c3, player) do | |
winner(a1, b1, c1, player, b2, c2, a3, b3, c3, next_player(player)) | |
end | |
def make_move("b2", a1, b1, c1, a2, " ", c2, a3, b3, c3, player) do | |
winner(a1, b1, c1, a2, player, c2, a3, b3, c3, next_player(player)) | |
end | |
def make_move("c2", a1, b1, c1, a2, b2, " ", a3, b3, c3, player) do | |
winner(a1, b1, c1, a2, b2, player, a3, b3, c3, next_player(player)) | |
end | |
def make_move("a3", a1, b1, c1, a2, b2, c2, " ", b3, c3, player) do | |
winner(a1, b1, c1, a2, b2, c2, player, b3, c3, next_player(player)) | |
end | |
def make_move("b3", a1, b1, c1, a2, b2, c2, a3, " ", c3, player) do | |
winner(a1, b1, c1, a2, b2, c2, a3, player, c3, next_player(player)) | |
end | |
def make_move("c3", a1, b1, c1, a2, b2, c2, a3, b3, " ", player) do | |
winner(a1, b1, c1, a2, b2, c2, a3, b3, player, next_player(player)) | |
end | |
def make_move(_move, a1, b1, c1, a2, b2, c2, a3, b3, c3, player) do | |
IO.puts "Invalid move." | |
move(a1, b1, c1, a2, b2, c2, a3, b3, c3, player) | |
end | |
def declare_winner(player) do | |
IO.puts("#{player} is the winner!") | |
end | |
def winner("X", "X", "X",_a2, _b2, _c2, _a3, _b3, _c3, _player) do | |
declare_winner("X") | |
end | |
def winner(_a1, _b1,_c1, "X", "X", "X", _a3, _b3, _c3, _player) do | |
declare_winner("X") | |
end | |
def winner(_a1, _b1, _c1, _a2, _b2, _c2, "X", "X", "X", _player) do | |
declare_winner("X") | |
end | |
def winner("X", _b1, _c1, "X", _b2, _c2, "X", _b3, _c3, _player) do | |
declare_winner("X") | |
end | |
def winner(_a1, "X",_c1, _a2, "X", _c2, _a3, "X", _c3, _player) do | |
declare_winner("X") | |
end | |
def winner(_a1, _b1, "X", _a2, _b2, "X", _a3, _b3, "X", _player) do | |
declare_winner("X") | |
end | |
def winner("X", _b1, _c1, _a2, "X", _c2, _a3, _b3, "X", _player) do | |
declare_winner("X") | |
end | |
def winner(_a1, _b1, "X", _a2, "X", _c2, "X", _b3, _c3, _player) do | |
declare_winner("X") | |
end | |
def winner("O", "O", "O",_a2, _b2, _c2, _a3, _b3, _c3, _player) do | |
declare_winner("O") | |
end | |
def winner(_a1, _b1,_c1, "O", "O", "O", _a3, _b3, _c3, _player) do | |
declare_winner("O") | |
end | |
def winner(_a1, _b1, _c1, _a2, _b2, _c2, "O", "O", "O", _player) do | |
declare_winner("O") | |
end | |
def winner("O", _b1, _c1, "O", _b2, _c2, "O", _b3, _c3, _player) do | |
declare_winner("O") | |
end | |
def winner(_a1, "O", _c1, _a2, "O", _c2, _a3, "O", _c3, _player) do | |
declare_winner("O") | |
end | |
def winner(_a1, _b1, "O", _a2, _b2, "O", _a3, _b3, "O", _player) do | |
declare_winner("O") | |
end | |
def winner("O", _b1, _c1, _a2, "O", _c2, _a3, _b3, "O", _player) do | |
declare_winner(O) | |
end | |
def winner(_a1, _b1, "O", _a2, "O", _c2, "O", _b3, _c3, _player) do | |
declare_winner(O) | |
end | |
def winner(a1, b1, c1, a2, b2, c2, a3, b3, c3, player) do | |
move(a1, b1, c1, a2, b2, c2, a3, b3, c3, player) | |
end | |
def next_player("X"), do: "O" | |
def next_player("O"), do: "X" | |
end | |
TicTacToe.play |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment