- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在井字游戏中,我想警告 O 玩家,如果 X 玩家选择 1-9 中的任何一个,他/她就会赢得游戏。
我的解决方案是在 O 玩家开始之前迭代 1-9。如果 1-9 中的任何一个会导致 X 赢得比赛,则通知 O 玩家他/她将输掉比赛,同时向他/她提供导致他/她输掉比赛的确切数字。
我的问题是我不知道如何在 //Player O 轮到
(第 87 行)之前迭代 1-9。
如果您有任何建议,这对我来说真的很有帮助。谢谢!
我的源代码如下;非常感谢您的帮助。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class TicTacToe {
public static void main(String[] args) {
new InputStreamReader(System.in);
BufferedReader theKeyboard = new BufferedReader(new InputStreamReader(System.in));
Board Game = new Board();
System.out.print("Enter 1 to play with computer;" + "\nEnter 2 to play with other people.\nPlease enter 1-2: ");
int players = 1;
String input = "";
boolean badInput = false;
do // get the number of players -- only accept 1 or 2
{
try {
input = theKeyboard.readLine();
} catch (IOException e) {
System.out.println("input error:" + e);
System.exit(1);
}
if (input.equals("1")) {
badInput = false;
players = 1;
} else if (input.equals("2")) {
badInput = false;
players = 2;
} else
badInput = true;
if (badInput)
System.out.print("Enter a number, 1 or 2: ");
} while (badInput);
System.out.println("TicTacToe Game starts." + " Please enter 1-9 to make your choice.");
int[] move = new int[2];
char winner;
int getTurn = 1; // The initialization of turns
System.out.println(Game); // print the board for first time
while (true) // loop only breaks when X or O wins, or a cat's game
{
// Player X's turn
if (getTurn % 2 != 0) {
if (players == 2) {
System.out.print("Player X, Enter 1-9 to make choice: ");
while (true) {
move = getMove();
// can't take occupied space
if (!Game.elementMarked(move[0], move[1]))
break;
System.out.println("That space is occupied.");
}
}
else // Or computer player
move = ComputerPlayer.makeMove(Game.copyBoard(), getTurn);
Game.markFirst(move[0], move[1]); // mark an X on the board
winner = Game.win(); // Check if win
if (winner != 'N')
break;
System.out.println(Game);
getTurn++; // return turn to the other player
}
// Player O's turn
System.out.print("Player O, Enter 1-9 to make choice: ");
while (true) {
move = getMove();
if (!Game.elementMarked(move[0], move[1]))
break;
System.out.println("This square has been chosen." + " Please enter a new square.");
}
Game.markSecond(move[0], move[1]);
winner = Game.win(); // Check if win
if (winner != 'N')
break;
System.out.println(Game);
getTurn++; // return turn to the other player
}
System.out.println(Game);
if (winner == 'C')
System.out.println("This is a cat's game.");
if (winner != 'C')
System.out.println("The winner is: " + winner);
}
// getMove gets the users choice and translates it into rows and columns
public static int[] getMove() {
new InputStreamReader(System.in);
BufferedReader theKeyboard = new BufferedReader(new InputStreamReader(System.in));
String input = "";
int[] move = new int[2];
boolean errorInput = false;
do {
try {
input = theKeyboard.readLine();
} catch (IOException e) {
System.out.println("input error:" + e);
System.exit(1);
}
if (input.equals("1")) {
move[0] = 0;
move[1] = 0;
errorInput = false;
} else if (input.equals("2")) {
move[0] = 0;
move[1] = 1;
errorInput = false;
} else if (input.equals("3")) {
move[0] = 0;
move[1] = 2;
errorInput = false;
} else if (input.equals("4")) {
move[0] = 1;
move[1] = 0;
errorInput = false;
} else if (input.equals("5")) {
move[0] = 1;
move[1] = 1;
errorInput = false;
} else if (input.equals("6")) {
move[0] = 1;
move[1] = 2;
errorInput = false;
} else if (input.equals("7")) {
move[0] = 2;
move[1] = 0;
errorInput = false;
} else if (input.equals("8")) {
move[0] = 2;
move[1] = 1;
errorInput = false;
} else if (input.equals("9")) {
move[0] = 2;
move[1] = 2;
errorInput = false;
} else
errorInput = true;
if (errorInput)
System.out.print("Error input. Enter a number within 1-9: ");
} while (errorInput);
return move;
}
}
/**
* ComputerPlayer is the AI client for computer, user can play with this "smart"
* computer.
*/
class ComputerPlayer {
public static int[] makeMove(int board[][], int turn) {
int square = 5;
int move[] = new int[2];
if (turn == 1) // first move is to get 5
{
square = 5;
move = Convert(square);
return move;
}
move = randomMove(board); // make a random move.
return move;
}
public static int[] randomMove(int board[][]) {
int move[] = new int[2];
int randomRow;
int randomCol;
while (true) {
randomRow = (int) (Math.random() * 3);
randomCol = (int) (Math.random() * 3);
if (board[randomRow][randomCol] == 0)
break;
}
move[0] = randomRow;
move[1] = randomCol;
return move;
}
// Convert will convert square (1-9) into a row and column
public static int[] Convert(int square) {
int move[] = new int[2];
if (square == 1) {
move[0] = 0;
move[1] = 0;
} else if (square == 2) {
move[0] = 0;
move[1] = 1;
} else if (square == 3) {
move[0] = 0;
move[1] = 2;
} else if (square == 4) {
move[0] = 1;
move[1] = 0;
} else if (square == 5) {
move[0] = 1;
move[1] = 1;
} else if (square == 6) {
move[0] = 1;
move[1] = 2;
} else if (square == 7) {
move[0] = 2;
move[1] = 0;
} else if (square == 8) {
move[0] = 2;
move[1] = 1;
} else if (square == 9) {
move[0] = 2;
move[1] = 2;
}
return move;
}
}
/**
* Board can represents 2D 3*3 array for TicTacToe game. It can check if someone
* wins or a cat's game. It can check if a square has been chosen. It can also
* mark an X or O from the player's choice.
*/
class Board {
private int[][] myBoard = new int[3][3];
// Create a 3 by 3 array and use for a tic tac toe board.
public Board() {
for (int row = 0; row < 3; row++) {
for (int column = 0; column < 3; column++) {
myBoard[row][column] = 0;
}
}
}
public int[][] copyBoard() {
return myBoard;
}
/*
* markFirst makes places a 2 accumulation for X
*/
public void markFirst(int row, int column) {
myBoard[row][column] = 2;
}
/*
* markSecond makes places a 1 accumulation for O
*/
public void markSecond(int row, int column) {
myBoard[row][column] = 1;
}
/*
* elementMarked returns a true if the space has been taken
*/
public boolean elementMarked(int row, int column) {
if (myBoard[row][column] == 0)
return false;
else
return true;
}
/*
* Win constructor checks if someone wins. Here are the meanings of each
* return type 'N' means no winner; 'X' means X won; 'O' means O won; 'C'
* means a C's game.
*/
public char win() {
char winner = 'N';
int catCheck = 1;
// Check the columns
for (int column = 0; column < 3; column++) {
int accumulation = myBoard[0][column] * myBoard[1][column] * myBoard[2][column];
if (accumulation == 8) // 2*2*2 = 8, a win for X
{
winner = 'X';
break;
}
if (accumulation == 1) // 1*1*1 = 1, a win for O
{
winner = 'O';
break;
}
}
if (winner != 'N')
return winner;
// Check the rows
for (int row = 0; row < 3; row++) {
int accumulation = myBoard[row][0] * myBoard[row][1] * myBoard[row][2];
if (accumulation == 8) {
winner = 'X';
break;
}
if (accumulation == 1) {
winner = 'O';
break;
}
}
if (winner != 'N')
return winner;
// Check one diagonal
int accumulation = myBoard[0][0] * myBoard[1][1] * myBoard[2][2];
if (accumulation == 1)
winner = 'O';
if (accumulation == 8)
winner = 'X';
// Check the other diagonal
accumulation = myBoard[0][2] * myBoard[1][1] * myBoard[2][0];
if (accumulation == 1)
winner = 'O';
if (accumulation == 8)
winner = 'X';
// If nobody's won, Check for a cat's game
if (winner == 'N') {
for (int row = 0; row < 3; row++) {
for (int column = 0; column < 3; column++) {
catCheck *= myBoard[row][column];
}
}
// any empty space is a zero. So product is zero if there is space
// left.
if (catCheck != 0)
winner = 'C';
}
return winner;
}
/*
* toString enables printing out of the board
*/
public String toString() {
String printBoard = "";
char XorO;
int position = 49; // In ASCII, 49 stands for number 1
for (int row = 0; row < 3; row++) {
for (int column = 0; column < 3; column++) {
if (myBoard[row][column] == 1)
// In ASCII, 79 stands for an O (78+1)
XorO = (char) (myBoard[row][column] + 78);
else if (myBoard[row][column] == 2)
// In ASCII, 88 stands for an X (86+2)
XorO = (char) (myBoard[row][column] + 86);
else
XorO = (char) (position);
position++;
printBoard = printBoard + XorO + " ";
}
printBoard = printBoard + "\n"; // starts a new line at the end of a
// row
}
return printBoard;
}
}
最佳答案
所以我想出的解决方案如下:为了检查 X(计算机)的下一步行动是否可能获胜,我们首先在棋盘的空闲单元中放置一个“理论/临时”(稍后会清楚为什么它是临时的)“X”。换句话说,我们一一迭代板的单元格。如果某个单元格空闲,我们会在其中放置一个临时“X”。然后我们考虑我们拥有的新董事会并检查是否有人获胜。如果是,我们清除放置临时“X”的单元格(这就是它被称为临时的原因)并返回一个长度为 2 的字符数组,其中包含谁赢了(即“X”、“O”、“C”或“N”) ')以及导致胜利的理论单元(1-9)。如果没有人获胜,我们将清除刚刚填充的单元格,并对下一个单元格再次尝试相同的过程,依此类推。
为了实现这一切,我编写了一个名为 canWin() 的方法,该方法基于 win() 方法构建,并进行了一些更改。此方法返回一个 2 元素字符数组,其中包含获胜者和位置(单元格索引(1-9))。该方法具体工作原理如下:该方法使用索引 i 的 for 循环,索引 i 从 1 递增到 9(单元格)。首先,使用您的转换方法,我们检查单元格 i 是否被占用:
if(!elementMarked(ComputerPlayer.Convert(i)[0], ComputerPlayer.Convert(i)[1]))
如果 elementMarked(...) 返回 true,我们将在循环中跳过此迭代并尝试以下单元格。但如果 elementMarked(...) 返回 false,那么我们将使用您的 markFirst(...) 方法在那里放置一个“X”:
markFirst(ComputerPlayer.Convert(i)[0], ComputerPlayer.Convert(i)[1]);
之后,使用您从 win() 检查的方式,我们检查是否有人获胜。如果确实如此,我们将获胜者和索引 i 保存在名为 result 的 2 元素 char 数组中,并返回 result。如果没有人获胜,则 i 增加并继续循环。这个方法看起来像这样:
public char [] canWin(){
char winner = 'N';
int catCheck = 1;
char [] result=new char[2];
for(int i=1; i<10; i++){
winner = 'N';
//Places an X if a cell is not occupied
if(!elementMarked(ComputerPlayer.Convert(i)[0], ComputerPlayer.Convert(i)[1]))
markFirst(ComputerPlayer.Convert(i)[0],ComputerPlayer.Convert(i)[1]);
else //If cell is occupied, skip this iteration
continue;
// Check the columns
for (int column = 0; column < 3; column++) {
int accumulation = myBoard[0][column] * myBoard[1][column] * myBoard[2][column];
if (accumulation == 8) // 2*2*2 = 8, a win for X
{
winner = 'X';
break;
}
if (accumulation == 1) // 1*1*1 = 1, a win for O
{
winner = 'O';
break;
}
}
if (winner != 'N'){
result[0] = winner; //register winner
result[1]=(char)i; //register cell that led to win
myBoard[ComputerPlayer.Convert(i)[0]][ComputerPlayer.Convert(i)[1]] = 0; //undoing the cell selection
return result;
}
// Check the rows
for (int row = 0; row < 3; row++) {
int accumulation = myBoard[row][0] * myBoard[row][1] * myBoard[row][2];
if (accumulation == 8) {
winner = 'X';
break;
}
if (accumulation == 1) {
winner = 'O';
break;
}
}
if (winner != 'N'){
result[0] = winner; //register winner
result[1]=(char)i; //register cell that led to win
myBoard[ComputerPlayer.Convert(i)[0]][ComputerPlayer.Convert(i)[1]] = 0; //undoing the cell selection
return result;
}
// Check one diagonal
int accumulation = myBoard[0][0] * myBoard[1][1] * myBoard[2][2];
if (accumulation == 1)
winner = 'O';
if (accumulation == 8){
winner = 'X';
result[0] = winner; //register winner
result[1]=(char)i; //register cell that led to win
myBoard[ComputerPlayer.Convert(i)[0]][ComputerPlayer.Convert(i)[1]] = 0; //undoing the cell selection
return result;
}
// Check the other diagonal
accumulation = myBoard[0][2] * myBoard[1][1] * myBoard[2][0];
if (accumulation == 1)
winner = 'O';
if (accumulation == 8){
winner = 'X';
result[0] = winner; //register winner
result[1]=(char)i; //register cell that led to win
myBoard[ComputerPlayer.Convert(i)[0]][ComputerPlayer.Convert(i)[1]] = 0; //undoing the cell selection
return result;
}
// If nobody's won, Check for a cat's game
if (winner == 'N') {
for (int row = 0; row < 3; row++) {
for (int column = 0; column < 3; column++) {
catCheck *= myBoard[row][column];
}
}
// any empty space is a zero. So product is zero if there is space
// left.
if (catCheck != 0)
winner = 'C';
}
result[0] = winner; //register winner
result[1]=(char)i; //register cell that led to win
myBoard[ComputerPlayer.Convert(i)[0]][ComputerPlayer.Convert(i)[1]] = 0; //undoing the cell selection
}
return result;
}
现在我们需要在你的主方法中实现这个方法。我们通过每次玩家 O 即将玩游戏时(即玩家 X 完成移动后)调用 canWin() 来做到这一点。如果 canWin() 返回“X”(即 X 可以通过下一步行动获胜),那么我们会向玩家打印一条消息,警告他并告诉他需要占据哪个单元格以防止 X 获胜。看起来像这样:
// Player O's turn
char[] canWin=Game.canWin();
if(canWin[0]=='X') //If X could win with his next move
System.out.println("Watch out! Player X can win if he plays in cell "+(int)Game.canWin()[1]+"!");
System.out.print("Player O, Enter 1-9 to make choice: ");
关于java - 如何在玩家开始之前迭代井字游戏中的整个方 block (1-9)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33771356/
我正在维护一些 Java 代码,我目前正在将它们转换为 C#。 Java 代码是这样做的: sendString(somedata + '\000'); 在 C# 中,我正在尝试做同样的事情: sen
如何确定函数中传递的参数是字符串还是字符(不确定如何正确调用它)文字? 我的函数(不正确): void check(const char* str) { // some code here }
我真的不知道如何准确地提出这个问题,但我希望标题已经说明了这一点。 我正在寻找一种方法(一个框架/库),它提供了执行 String.contains() 函数的能力,该函数告诉我给定的字符串是否与搜索
我正在尝试编写一些读取 Lambda 表达式并输出 beta 缩减版本的东西。 Lambda 的类型如下:\variable -> expression,应用程序的形式为 (表达式) (表达式)。因此
StackOverflow 上的第 1 篇文章,如果我没能把它做好,我深表歉意。我陷入了一个愚蠢的练习,我需要制作一个“刽子手游戏”,我尝试从“.txt”文件中读取单词,然后我得到了我的加密函数,它将
我想在 Groovy 中测试我的 Java 自定义注释,但由于字符问题而未能成功。 Groovyc: Expected 'a' to be an inline constant of type cha
当我尝试在单击按钮期间运行 javascript location.href 时,出现以下错误“字 rune 字中的字符过多”。 最佳答案 这应该使用 OnClientClick相反? 您可能还想停
我想要类似的东西: let a = ["v".utf8[0], 1, 2] 我想到的最接近的是: let a = [0x76, 1, 2] 和 "v".data(using: String.Encod
有没有办法在 MySQL 中指定 Unicode 字 rune 字? 我想用 Ascii 字符替换 Unicode 字符,如下所示: Update MyTbl Set MyFld = Replace(
阅读 PNG 规范后,我有点惊讶。我读过字 rune 字应该用像 0x41 这样的二进制值进行硬编码,而不是在(程序员友好的)'A' 中。问题似乎是在具有不同底层字符集的不同系统上编译期间字 rune
考虑一个具有 UTF-8 执行字符集的 C++11 编译器(并且符合要求 char 类型为有符号 8 位字节的 x86-64 ABI) . 字母 Ä(元音变音)具有 0xC4 的 unicode 代码
为什么即使有 UTF-8 字符串文字,C11 或 C++11 中也没有 UTF-8 字 rune 字?我知道,一般来说,字 rune 字表示单个 ASCII 字符,它与单字节 UTF-8 代码点相同,
我怎样才能用 Jade 做到这一点? how would I do this 我几乎可以做任何事情,除了引入一个 span 中间句子。 最佳答案 h3.blur. how would I do t
这似乎是一个非常简单的问题,但我只是想澄清我的疑问。我正在查看其他开发人员编写的代码。有一些涉及 float 的计算。 示例:Float fNotAvlbl = new Float(-99); 他为什
我想知道第 3 行“if dec:”中的“dec”是什么意思 1 def dec2bin(dec): 2 result='' 3 if dec:
我试图在字符串中查找不包含任何“a”字符的单词。我写了下面的代码,但它不起作用。我怎么能对正则表达式说“不包括”?我不能用“^”符号表示“不是”吗? import re string2 = "asfd
这个问题在这里已经有了答案: Is floating point math broken? (31 个答案) Is floating point arbitrary precision availa
我正在创建一个时尚的文本应用程序,但在某些地方出现错误(“字 rune 字中的字符太多”)。我只写了一个字母,但是当我粘贴它时,它会转换成许多这样的字母:“\uD83C\uDD89”,原始字母是“🆉
我正在尝试检查用户是否在文本框中输入了一个数字值,是否接受了小数位。非常感谢任何帮助。 Private Sub textbox1_AfterUpdate() If IsNumeric(textbox1
我知道一个 Byte 是 8 位,但其他的代表什么?我正在参加一个使用摩托罗拉 68k 架构的汇编类(class),我对目前的词汇感到困惑。 最佳答案 如 operator's manual for
我是一名优秀的程序员,十分优秀!