井字棋盘

# coding: utf-8
# -----------------棋盘的定义------------------------------------------
theBoard = {'top_L': ' ', 'top_M': ' ', 'top_R': ' ',
            'mid_L': ' ', 'mid_M': ' ', 'mid_R': ' ',
            'low_L': ' ', 'low_M': ' ', 'low_R': ' '}


# ----------------棋盘刻画函数----------------------------------------
def printBoard(board):
    """将棋盘刻画出来"""
    print(board['top_L'] + '|' + board['top_M'] + '|' + board['top_R'])
    print('-+-+-')
    print(board['mid_L'] + '|' + board['mid_M'] + '|' + board['mid_R'])
    print('-+-+-')
    print(board['low_L'] + '|' + board['low_M'] + '|' + board['low_R'])

    return board


# -------------------玩家输入下棋-----------------------------------------
player_1 = 'O'
player_2 = 'X'
player = player_1
for time in range(9):
    board = printBoard(theBoard)
    print(board)

    # ---------------------判断胜利--------------------------------------------------------
    if theBoard['top_L'] == theBoard['top_M'] == theBoard['top_R'] == player_1:
        print('play_1 wins')
        break
    elif theBoard['top_L'] == theBoard['top_M'] == theBoard['top_R'] == player_1:
        print('play_2 wins')
        break
    elif theBoard['mid_L'] == theBoard['mid_M'] == theBoard['mid_R'] == player_1:
        print('play_2 wins')
        break
    elif theBoard['low_L'] == theBoard['low_M'] == theBoard['low_R'] == player_1:
        print('play_2 wins')
        break
    elif theBoard['top_L'] == theBoard['mid_M'] == theBoard['low_R'] == player_1:
        print('play_2 wins')
        break
    elif theBoard['top_R'] == theBoard['mid_M'] == theBoard['low_L'] == player_1:
        print('play_2 wins')
        break

    elif theBoard['top_L'] == theBoard['top_M'] == theBoard['top_R'] == player_2:
        print('play_2 wins')
        break
    elif theBoard['mid_L'] == theBoard['mid_M'] == theBoard['mid_R'] == player_2:
        print('play_2 wins')
        break
    elif theBoard['low_L'] == theBoard['low_M'] == theBoard['low_R'] == player_2:
        print('play_2 wins')
        break
    elif theBoard['top_L'] == theBoard['mid_M'] == theBoard['low_R'] == player_2:
        print('play_2 wins')
        break
    elif theBoard['top_R'] == theBoard['mid_M'] == theBoard['low_L'] == player_2:
        print('play_2 wins')
        break
        
    # -------------------------------------------------------------------------------------
    print("Choose which position do you want to set? (top_L, top_M, top_R; mid_L, mid_M, mid_R; low_L, low_M, low_R)")
    move = input()
    theBoard[move] = player
    if player == player_1:
        player = player_2
    elif player == player_2:
        player = player_1


在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,作为AI语言模型,我不能编写和运行Java程序。但是,我可以提供一个基本的代码结构,帮助您开始编写井字棋游戏。 ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Tictactoe extends JFrame implements ActionListener { // 定义菜单栏和菜单项 private JMenuBar menuBar; private JMenu operationMenu; private JMenuItem startItem; private JMenuItem saveItem; private JMenuItem exitItem; // 定义界面组件 private JComboBox boardSizeComboBox; private JButton startButton; private JButton restartButton; // ... // 定义游戏逻辑 private int[][] board; // 用二维数组来表示棋盘 private int currentPlayer; // 当前玩家,1表示玩家1,2表示玩家2 // ... public Tictactoe() { // 初始化菜单栏和菜单项 menuBar = new JMenuBar(); operationMenu = new JMenu("操作"); startItem = new JMenuItem("开始游戏"); saveItem = new JMenuItem("保存结果"); exitItem = new JMenuItem("退出游戏"); // ... // 初始化界面组件 boardSizeComboBox = new JComboBox(new String[]{"3x3", "4x4", "5x5", "6x6", "7x7", "8x8", "9x9", "10x10"}); startButton = new JButton("开始"); restartButton = new JButton("再来一局"); // ... // 设置布局和添加组件 // ... // 添加事件监听器 startItem.addActionListener(this); saveItem.addActionListener(this); exitItem.addActionListener(this); startButton.addActionListener(this); restartButton.addActionListener(this); // ... // 设置窗口属性 // ... } public void actionPerformed(ActionEvent e) { if (e.getSource() == startItem) { // 处理开始游戏菜单项的点击事件 // ... } else if (e.getSource() == saveItem) { // 处理保存结果菜单项的点击事件 // ... } else if (e.getSource() == exitItem) { // 处理退出游戏菜单项的点击事件 // ... } else if (e.getSource() == startButton) { // 处理开始按钮的点击事件 // ... } else if (e.getSource() == restartButton) { // 处理再来一局按钮的点击事件 // ... } } // 实现游戏逻辑,包括玩家操作和电脑操作 // ... public static void main(String[] args) { Tictactoe game = new Tictactoe(); game.setVisible(true); } } ``` 在该代码结构中,您需要完成以下任务: 1. 初始化菜单栏、菜单项和界面组件。 2. 处理菜单项、开始按钮和再来一局按钮的点击事件。 3. 实现游戏逻辑,包括玩家操作和电脑操作。 4. 实现保存结果功能,将每局对战结果追加写入当前目录下myText.txt文件。 5. 实现退出游戏功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值