Created
March 10, 2019 09:29
-
-
Save bingyuanng/e671bdd5d2bb2e11832a8fa7eda6ce77 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
$board = {} | |
$turn = 0 | |
def main() | |
# display board | |
# player 1 select input | |
# display board | |
# player 2 select input | |
until $turn >= 9 | |
showBoard() | |
input = getInput() | |
fillSquare(input, $turn) | |
$turn+=1 | |
end | |
end | |
def getInput() | |
input = -1 | |
until input >= 0 && input < 9 | |
begin | |
puts "Please select a square [0-8]" | |
input = Integer(gets) | |
rescue | |
puts "Please insert only the position you wish the select [0-8]" | |
input = -1 | |
end | |
end | |
return input | |
end | |
def fillSquare(index, turn) | |
$board[index] = turn % 2 | |
end | |
def showSquare(index) | |
if $board[index].nil? | |
return index | |
end | |
return $board[index] === 1 ? "X" : "O" | |
end | |
def showBoard() | |
puts "-------------" | |
puts "| #{showSquare(0)} | #{showSquare(1)} | #{showSquare(2)} |" | |
puts "-------------" | |
puts "| #{showSquare(3)} | #{showSquare(4)} | #{showSquare(5)} |" | |
puts "----------" | |
puts "| #{showSquare(6)} | #{showSquare(7)} | #{showSquare(8)} |" | |
puts "-------------" | |
end | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment