IN PYTHON Pleases!! B. Tic-Tac-Toe The game of Tic-Tac-Toe (or Naughts and Cross
ID: 3924333 • Letter: I
Question
IN PYTHON Pleases!!
B. Tic-Tac-Toe The game of Tic-Tac-Toe (or Naughts and Crosses) is an ancient game in which 2 players alternate turns placing either X's or O's on a 3x3 square grid. One player places X's (crosses) on the grid and the opposing player places O's (naughts). The first player to place 3 consecutive X's (or O's) on any row, column or diagonal of the grid wins the game. If all 9 grid cells are occupied without either player winning, the game is declared a draw.
For this problem, you will represent the game 'grid' using a multidimensional (nested) list. The grid representation consists of a 3-element list in which each element represents a row. Each row is another list representing the three columns. Unoccupied grid locations are represented using null strings. For example, this is how the grid would appear before anyone makes a move:
[['', '', ''], ['', '', ''], ['', '', '']]
Given the following game state (after several 'moves'):
O X
X O
X O
The resulting grid list would be as follows:
[['O', '', 'X'], ['X', 'O', ''], ['X', '', 'O']]
Write a pure function named gameState that takes a single list argument representing the grid and determines the current state of the game: player X has won, player O has won, the game is a draw, or none of those. Your function should return a single character string as follows: 'X' if player X has won the game, 'O' if player O has won the game, 'D' if the game is a draw (all cells occupied but neither player has won) and "" (null string) if none of these conditions exist. (Hint: use a loop to compare each element of the main diagonal with all the elements of its intersecting row and column, and then check the two diagonals)
Write a separate non-pure function (procedure) named testTTT that will test your function as follows:
1. Represent the four game configurations as nested lists using the format described above. You do not have to input them into your procedure, simply declare them:
X Wins:
X O
X O
X O
O Wins:
O X
X O
X O
Draw:
O X O
X X O
X O X
No wins,
No draw:
X O
X O
X O
2. Call the gameState function to determine the game state.
3. Print the appropriate message for each game state (as determined by the gameState function):
"X wins"
"O wins"
"Draw"
"NO Win, No Draw"
Explanation / Answer
def print_board(board):
print "The board look like this: "
for i in range(3):
print " ",
for j in range(3):
if board[i*3+j] == 1:
print 'X',
elif board[i*3+j] == 0:
print 'O',
elif board[i*3+j] != -1:
print board[i*3+j]-1,
else:
print ' ',
if j != 2:
print " | ",
print
if i != 2:
print "-----------------"
else:
print
def print_instruction():
print "Please use the following cell numbers to make your move"
print_board([2,3,4,5,6,7,8,9,10])
def get_input(turn):
valid = False
while not valid:
try:
user = raw_input("Where would you like to place " + turn + " (1-9)? ")
user = int(user)
if user >= 1 and user <= 9:
return user-1
else:
print "That is not a valid move! Please try again. "
print_instruction()
except Exception as e:
print user + " is not a valid move! Please try again. "
def check_win(board):
win_cond = ((1,2,3),(4,5,6),(7,8,9),(1,4,7),(2,5,8),(3,6,9),(1,5,9),(3,5,7))
for each in win_cond:
try:
if board[each[0]-1] == board[each[1]-1] and board[each[1]-1] == board[each[2]-1]:
return board[each[0]-1]
except:
pass
return -1
def quit_game(board,msg):
print_board(board)
print msg
quit()
def main():
# Start Game
# Change turns
# Checks for winner
# Quits and redo board
print_instruction()
board = []
for i in range(9):
board.append(-1)
win = False
move = 0
while not win:
# Print board
print_board(board)
print "Turn number " + str(move+1)
if move % 2 == 0:
turn = 'X'
else:
turn = 'O'
# Get player input
user = get_input(turn)
while board[user] != -1:
print "Invalid move! Cell already taken. Please try again. "
user = get_input(turn)
board[user] = 1 if turn == 'X' else 0
# Continue move and check if end of game
move += 1
if move > 4:
winner = check_win(board)
if winner != -1:
out = "The winner is "
out += "X" if winner == 1 else "O"
out += ""
quit_game(board,out)
elif move == 9:
quit_game(board,"No winner")
if __name__ == "__main__":
main()