Created
June 19, 2019 20:02
-
-
Save JEG2/4a6363e4c1093b4c1698a0965997497e 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 | |
move(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 | |
move(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 | |
move(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 | |
move(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 | |
move(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 | |
move(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 | |
move(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 | |
move(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 | |
move(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 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