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]]
|
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"
|
print "\n -------------\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def checkWin(board, x, o)
|
||||||
#if x is in lines then x wins
|
for line in LINES do
|
||||||
#if o is in lines then x wins
|
if board[line[0]] == "X" and board[line[1]] == "X" and board[line[2]] == "X"
|
||||||
#if no nums in board then game over
|
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"
|
player = "X"
|
||||||
loop do
|
x = Array.new
|
||||||
|
o = Array.new
|
||||||
|
def play(board, player, x, o)
|
||||||
|
printBoard(board)
|
||||||
print "Place an #{player} "
|
print "Place an #{player} "
|
||||||
input = gets.to_i
|
input = gets.to_i
|
||||||
p input
|
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
|
board[input] = player
|
||||||
printBoard(board)
|
|
||||||
if player == "X"
|
if player == "X"
|
||||||
|
x.push(input)
|
||||||
player = "O"
|
player = "O"
|
||||||
else
|
else
|
||||||
|
o.push(input)
|
||||||
player = "X"
|
player = "X"
|
||||||
end
|
end
|
||||||
|
checkWin(board, x, o)
|
||||||
|
play(board, player, x, o)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
play(board, player, x, o)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user