井字棋--python实现

一.要求

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:

  1. The game is always played on a 3x3 grid.
  2. At the start, the ‘player’ (e.g. you) will select an appropriate shape1, either ‘X’ or ‘O’.
  3. The player will always begin the game.
  4. The computer will automatically get the remaining shape (i.e. if you selected ‘X’, the computer will
    play ‘O’).
  5. 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).
  6. 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 
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值