java井字棋编程的收获_Java 井字棋小结

1.井字棋获胜的四种情况

横行全为同一符号

竖行全为同一符号

斜对角线为同一符号

反对角线为同一符号

2.编程思路

构建数组(二维)读入数据

分四个部分,分别判断横行,竖行,对角线,反对角线是否满足条件

输出结果,存在两种情况,有胜负,无胜负

3.上代码

import java.util.Scanner;

public class 井字棋 {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner in = new Scanner(System.in);

int size = 3;

int [][] board = new int[3][3];

boolean getresult = false;

int xnum = 0;

int ynum = 0;

for(int i=0;i

{

//内层循环的board[i].length是防止索引超出范围,若是j.length会出现board[3].length的情况

for(int j=0;j

{

board[i][j] = in.nextInt();

}

}

for(int i=0;i

{

for(int j=0;j

{

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的井字棋 Java 代码示例: ```java import java.util.Scanner; public class TicTacToe { private char[][] board; // 棋盘 private char currentPlayer; // 当前玩家 public TicTacToe() { board = new char[3][3]; currentPlayer = 'X'; initializeBoard(); } // 初始化棋盘 public void initializeBoard() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { board[i][j] = '-'; } } } // 打印棋盘 public void printBoard() { System.out.println("-------------"); for (int i = 0; i < 3; i++) { System.out.print("| "); for (int j = 0; j < 3; j++) { System.out.print(board[i][j] + " | "); } System.out.println(); System.out.println("-------------"); } } // 切换玩家 public void changePlayer() { currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; } // 检查是否已经有人胜利 public boolean checkForWin() { return (checkRowsForWin() || checkColumnsForWin() || checkDiagonalsForWin()); } // 检查行是否有胜利者 public boolean checkRowsForWin() { for (int i = 0; i < 3; i++) { if (checkRowCol(board[i][0], board[i][1], board[i][2])) { return true; } } return false; } // 检查列是否有胜利者 public boolean checkColumnsForWin() { for (int i = 0; i < 3; i++) { if (checkRowCol(board[0][i], board[1][i], board[2][i])) { return true; } } return false; } // 检查对角线是否有胜利者 public boolean checkDiagonalsForWin() { return (checkRowCol(board[0][0], board[1][1], board[2][2]) || checkRowCol(board[0][2], board[1][1], board[2][0])); } // 检查行或列是否有连续三个相同的字符 public boolean checkRowCol(char c1, char c2, char c3) { return ((c1 != '-') && (c1 == c2) && (c2 == c3)); } // 下棋 public void placeMark(int row, int col) { if ((row >= 0) && (row < 3) && (col >= 0) && (col < 3)) { if (board[row][col] == '-') { board[row][col] = currentPlayer; } } } // 运行游戏 public void play() { Scanner scanner = new Scanner(System.in); boolean gameOver = false; System.out.println("Let's play Tic Tac Toe!"); while (!gameOver) { System.out.println("Player " + currentPlayer + ", enter a row (0, 1, or 2): "); int row = scanner.nextInt(); System.out.println("Player " + currentPlayer + ", enter a column (0, 1, or 2): "); int col = scanner.nextInt(); placeMark(row, col); printBoard(); if (checkForWin()) { System.out.println("Congratulations! Player " + currentPlayer + " wins!"); gameOver = true; } else if (boardFull()) { System.out.println("Game over! It's a tie!"); gameOver = true; } else { changePlayer(); } } } // 检查棋盘是否已满 public boolean boardFull() { boolean full = true; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board[i][j] == '-') { full = false; } } } return full; } public static void main(String[] args) { TicTacToe game = new TicTacToe(); game.play(); } } ``` 这个示例中,我们创建了一个 `TicTacToe` 类,表示一个井字棋游戏。在 `TicTacToe` 类中,我们定义了一些方法,如 `initializeBoard` 方法用于初始化棋盘,`printBoard` 方法用于打印棋盘,`changePlayer` 方法用于切换玩家,`checkForWin` 方法用于检查是否已经有人胜利等等。我们还在 `main` 方法中创建了一个 `TicTacToe` 对象并调用了 `play` 方法来运行游戏。 在游戏运行过程中,我们使用 `Scanner` 类来读取玩家的输入。玩家在每轮中输入一个行号和列号,程序会根据这个输入来下棋。程序会在每轮结束时检查是否有人胜利或棋盘已满,如果有人胜利或棋盘已满,游戏结束。如果没有人胜利或棋盘未满,程序会切换到下一个玩家并进行下一轮游戏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值