added win conditions
This commit is contained in:
parent
7865ae49de
commit
5b09ea5b56
36
tik.rb
36
tik.rb
@ -1,5 +1,4 @@
|
||||
#tiktaktoe
|
||||
|
||||
#tiktaktoe 01/17/2022
|
||||
|
||||
LINES = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]
|
||||
|
||||
@ -18,22 +17,41 @@ def printBoard(board)
|
||||
print "\n -------------\n"
|
||||
end
|
||||
|
||||
|
||||
#if x is in lines then x wins
|
||||
#if o is in lines then x wins
|
||||
#if no nums in board then game over
|
||||
def checkWin(board, x, o)
|
||||
for line in LINES do
|
||||
if board[line[0]] == "X" and board[line[1]] == "X" and board[line[2]] == "X"
|
||||
puts "X WINS!!!!!!"
|
||||
exit
|
||||
elsif board[line[0]] == "X" and board[line[1]] == "X" and board[line[2]] == "X"
|
||||
puts "O WINS!!!!!!"
|
||||
exit
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
player = "X"
|
||||
loop do
|
||||
x = Array.new
|
||||
o = Array.new
|
||||
def play(board, player, x, o)
|
||||
printBoard(board)
|
||||
print "Place an #{player} "
|
||||
input = gets.to_i
|
||||
p input
|
||||
p board.include?(input)
|
||||
if board.include?(input)
|
||||
else
|
||||
puts "Bad input, pick a valid position"
|
||||
play(board, player, x, o)
|
||||
end
|
||||
board[input] = player
|
||||
printBoard(board)
|
||||
if player == "X"
|
||||
x.push(input)
|
||||
player = "O"
|
||||
else
|
||||
o.push(input)
|
||||
player = "X"
|
||||
end
|
||||
checkWin(board, x, o)
|
||||
play(board, player, x, o)
|
||||
end
|
||||
|
||||
play(board, player, x, o)
|
||||
|
Loading…
x
Reference in New Issue
Block a user