一.要求
For this assignment, you will implement the game of Noughts and Crosses. Your assignment solution will
allow a person to play this game against your computer.
You can learn about the game in detail here (if you are not already familiar). The rules that we will follow are
straightforward:
- The game is always played on a 3x3 grid.
- At the start, the ‘player’ (e.g. you) will select an appropriate shape1, either ‘X’ or ‘O’.
- The player will always begin the game.
- The computer will automatically get the remaining shape (i.e. if you selected ‘X’, the computer will
play ‘O’). - The player who has his/her own shape thrice in a row, column or diagonal, will win the game (the
game will notify who is the winner and terminates). - In case of a draw, the game will notify the player that the game has drawn, and terminate.
以上的内容实际上就是让写一个与电脑之间的井字棋比赛
二.代码实现
import random
# 创建棋盘
def create_board():
board = [[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']]
return board
# 行信息
def row_information(board, row_number):
list = []
for i in range(3):
list.append(board[row_number][i])
return list
# 列信息
def column_information(board,col_number):
list = []
for i in range(3):
list.append(board[i][col_number])
return list
# 对角线信息
def diagonal_information(board,selected_diagonal):
list = []
if(selected_diagonal == 0):
for i in range(3):
list.append(board[i][i])
else:
for i in range(2,-1):
list.append(board[i][i])
return list
# 所有空的集合
def empty_cells(board):
list = [];
for i