java tictactoe_一个井字棋tictactoe游戏的java实现 | Soo Smart!

这是一个井字棋游戏的java实现。摘录于stackoverflow。

游戏规则很简单,只要一方棋子在水平线,垂直线或者对角线任意一条线上排列成功即为获胜。

作者原先的代码存在着一些问题:

代码如下:

一共有几个类: play, player, human, computer, set

Play类:

import java.util.Scanner;

public class play {

public static void main(String[] args) {

System.out.println("Welcome to Tickle Tackle Toe!!! :D");

System.out.println();

//creat markers

String marker1 = "x";

String marker2 = "o";

boolean playAgain = true;

Scanner s = new Scanner(System.in);

//create player objects

Human human = new Human();

Computer computer = new Computer();

while(playAgain){

//run board setup

set Setup = new set();

Setup.createBoard();

Setup.printBoard();

System.out.println("please choose your marker");

System.out.println("type 1 for 'x' or 2 for 'o'");

//set markers

if(s.nextInt() == 1){

// create player objects

human.setMarker("x");

computer.setMarker("o");

}

else

{

human.setMarker("o");

computer.setMarker("x");

}

// determine who goes first

int first = (int) (Math.random() * 2);

boolean won = false;

int turns = 0;

if(first == 0){

System.out.println("You go first!");

System.out.println();

while(!won){

human.takeTurn(Setup.getBoard());

turns++;

Setup.printBoard();

if(Setup.hasWon(Setup.getBoard())){

won = true;

System.out.println("Congrats you won!");

}

if(turns == 9){

won = true;

System.out.println("Its a draw!");

break;

}

if(!won){

computer.takeTurn(Setup.getBoard(), human);

turns++;

System.out.println();

Setup.printBoard();

System.out.println();

if(Setup.hasWon(Setup.getBoard())){

won = true;

System.out.println("You lost!");

}

if(turns == 9){

won = true;

System.out.println("Its a draw!");

break;

}

}

} // close while 1

}

else {

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值