- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
伙计们,我已经从头开始完成 TicTacToe 游戏的代码,如果任何玩家在一行中水平、垂直或对角线获得相同的值,它就可以正常工作。但是,如果比赛是平局,例如:
X O X
O X X
O X O
Player #2 (O) enter the row and column numbers:
尽管我在 count
中设置了限制,但代码仍会要求用户输入。用户必须输入总共 9
个输入字符,因为 TicTacToe 中有 9
个框。每次用户输入 row
和 col
数字时,count
都会增加 1
。我的 while-loop
看起来像这样:
while(!(count > 9)){
.....................
}
我的 while
循环有什么问题吗?
import java.util.Scanner;
import java.util.*;
public class TicTacToe {
public static final char[][] theBoard = new char[4][4];
static Scanner kbd = new Scanner(System.in);
public static void main(String[] args){
System.out.println("Hello! Welcome to TicTacToe!");
System.out.println("Player #1 is X! \nPlayer #2 is Y!");
System.out.println();
getBoard();
System.out.println();
playGame();
}
public static void getBoard(){
for(int i = 1; i < theBoard.length; i++){
for(int j = 1; j < theBoard[i].length; j++){
theBoard[i][j] = '_';
System.out.print(theBoard[i][j] + " ");
}
System.out.println();
}
}
public static void playGame(){
int row = 0;
int col = 0;
boolean check = false;
int count = 0;
while(!(count > 9)){
System.out.println();
System.out.println("Player #1 (X) enter the row and column numbers: ");
row = kbd.nextInt();
col = kbd.nextInt();
System.out.println();
count++;
if (row < 0 || col < 0 || row > theBoard.length
|| col > theBoard.length) {
throw new RuntimeException("The numbers should be in range 1-3");
}
else if (theBoard[row][col] != '_') {
throw new RuntimeException("The space already filled!");
}
else {
theBoard[row][col] = 'X';
for (int i = 1; i < theBoard.length; i++) {
for (int j = 1; j < theBoard.length; j++) {
System.out.print(theBoard[i][j] + " ");
}
System.out.println();
}
}
check = checkIfWin();
if(check == true){
break;
}
System.out.println();
System.out.println("Player #2 (O) enter the row and column numbers: ");
row = kbd.nextInt();
col = kbd.nextInt();
System.out.println();
count++;
if (row < 0 || col < 0 || row > theBoard.length
|| col > theBoard.length) {
throw new RuntimeException("The numbers should be in range 1-3");
}
else if (theBoard[row][col] != '_') {
throw new RuntimeException("The space already filled!");
}
else {
theBoard[row][col] = 'O';
for (int i = 1; i < theBoard.length; i++) {
for (int j = 1; j < theBoard.length; j++) {
System.out.print(theBoard[i][j] + " ");
}
System.out.println();
}
}
check = checkIfWin();
if(check == true){
break;
}
}
System.out.println("The game is tie!");
}
private static boolean checkIfWin(){
boolean result = false;
int countX = 0;
int countO = 0;
//Check 1st line
for(int i = 1; i < theBoard.length; i++){
if(theBoard[1][i] == 'X'){
countX++;
}
else if(theBoard[1][i] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
countX = 0;
countO = 0;
//Check 2nd line
for(int i = 1; i < theBoard.length; i++){
if(theBoard[2][i] == 'X'){
countX++;
}
else if(theBoard[2][i] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
countX = 0;
countO = 0;
//Check 3d line
for(int i = 1; i < theBoard.length; i++){
if(theBoard[3][i] == 'X'){
countX++;
}
else if(theBoard[3][i] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
countX = 0;
countO = 0;
//Check 1s column
for(int i = 1; i < theBoard.length; i++){
if(theBoard[i][1] == 'X'){
countX++;
}
else if(theBoard[i][1] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
countX = 0;
countO = 0;
//Check 2nd column
for(int i = 1; i < theBoard.length; i++){
if(theBoard[i][2] == 'X'){
countX++;
}
else if(theBoard[i][2] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
countX = 0;
countO = 0;
//Check 3d column
for(int i = 1; i < theBoard.length; i++){
if(theBoard[i][3] == 'X'){
countX++;
}
else if(theBoard[i][3] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
//Check first diagonal
if(theBoard[1][1] == 'X' && theBoard[2][2] == 'X' && theBoard[3][3] == 'X'){
result = true;
System.out.println("The Player #1 (X) Wins!");
}
else if(theBoard[1][1] == 'O' && theBoard[2][2] == 'O' && theBoard[3][3] == 'O'){
result = true;
System.out.println("The Player #2 (O) Wins!");
}
// Check first diagonal
if (theBoard[1][3] == 'X' && theBoard[2][2] == 'X'&& theBoard[3][1] == 'X'){
result = true;
System.out.println("The Player #1 (X) Wins!");
} else if (theBoard[1][3] == 'O' && theBoard[2][2] == 'O'&& theBoard[3][1] == 'O') {
result = true;
System.out.println("The Player #2 (O) Wins!");
}
return result;
}
}
最佳答案
你的问题源于这样一个事实,即你给了每个玩家离开的机会。您需要在循环中间添加另一个检查。在 Tic Tac Toe 中,并非每个玩家的轮次都相等,因此您是否在每个人都离开后检查,而不是在游戏完成时检查。
你需要在这里再检查一次(在我的代码中是第 67 行):
check = checkIfWin();
if((check == true) || (count >= 8)){
break;
}
这个程序有效:
package tictactoe;
import java.util.Scanner;
import java.util.*;
public class TicTacToe {
public static final char[][] theBoard = new char[4][4];
static Scanner kbd = new Scanner(System.in);
public static void main(String[] args){
System.out.println("Hello! Welcome to TicTacToe!");
System.out.println("Player #1 is X! \nPlayer #2 is Y!");
System.out.println();
getBoard();
System.out.println();
playGame();
}
public static void getBoard(){
for(int i = 1; i < theBoard.length; i++){
for(int j = 1; j < theBoard[i].length; j++){
theBoard[i][j] = '_';
System.out.print(theBoard[i][j] + " ");
}
System.out.println();
}
}
public static void playGame(){
int row = 0;
int col = 0;
boolean check = false;
int count = 0;
//change this
while(count <= 8){
count++;
System.out.println();
System.out.println("Player #1 (X) enter the row and column numbers: ");
row = kbd.nextInt();
col = kbd.nextInt();
System.out.println();
if (row < 0 || col < 0 || row > theBoard.length
|| col > theBoard.length) {
throw new RuntimeException("The numbers should be in range 1-3");
}
else if (theBoard[row][col] != '_') {
throw new RuntimeException("The space already filled!");
}
else {
theBoard[row][col] = 'X';
for (int i = 1; i < theBoard.length; i++) {
for (int j = 1; j < theBoard.length; j++) {
System.out.print(theBoard[i][j] + " ");
}
System.out.println();
}
}
check = checkIfWin();
//change this
if((check == true) || (count >= 8)){
break;
}
System.out.println();
System.out.println("Player #2 (O) enter the row and column numbers: ");
row = kbd.nextInt();
col = kbd.nextInt();
System.out.println();
count++;
if (row < 0 || col < 0 || row > theBoard.length
|| col > theBoard.length) {
throw new RuntimeException("The numbers should be in range 1-3");
}
else if (theBoard[row][col] != '_') {
throw new RuntimeException("The space already filled!");
}
else {
theBoard[row][col] = 'O';
for (int i = 1; i < theBoard.length; i++) {
for (int j = 1; j < theBoard.length; j++) {
System.out.print(theBoard[i][j] + " ");
}
System.out.println();
}
}
check = checkIfWin();
if(check == true){
break;
}
}
System.out.println("The game is tie!");
}
private static boolean checkIfWin(){
boolean result = false;
int countX = 0;
int countO = 0;
//Check 1st line
for(int i = 1; i < theBoard.length; i++){
if(theBoard[1][i] == 'X'){
countX++;
}
else if(theBoard[1][i] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
countX = 0;
countO = 0;
//Check 2nd line
for(int i = 1; i < theBoard.length; i++){
if(theBoard[2][i] == 'X'){
countX++;
}
else if(theBoard[2][i] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
countX = 0;
countO = 0;
//Check 3d line
for(int i = 1; i < theBoard.length; i++){
if(theBoard[3][i] == 'X'){
countX++;
}
else if(theBoard[3][i] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
countX = 0;
countO = 0;
//Check 1s column
for(int i = 1; i < theBoard.length; i++){
if(theBoard[i][1] == 'X'){
countX++;
}
else if(theBoard[i][1] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
countX = 0;
countO = 0;
//Check 2nd column
for(int i = 1; i < theBoard.length; i++){
if(theBoard[i][2] == 'X'){
countX++;
}
else if(theBoard[i][2] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
countX = 0;
countO = 0;
//Check 3d column
for(int i = 1; i < theBoard.length; i++){
if(theBoard[i][3] == 'X'){
countX++;
}
else if(theBoard[i][3] == 'O'){
countO++;
}
if(countX == 3){
result = true;
System.out.println("The Player #1 (X) Wins!");
break;
}
else if(countO == 3){
result = true;
System.out.println("The Player #2 (O) Wins!");
break;
}
}
//Check first diagonal
if(theBoard[1][1] == 'X' && theBoard[2][2] == 'X' && theBoard[3][3] == 'X'){
result = true;
System.out.println("The Player #1 (X) Wins!");
}
else if(theBoard[1][1] == 'O' && theBoard[2][2] == 'O' && theBoard[3][3] == 'O'){
result = true;
System.out.println("The Player #2 (O) Wins!");
}
// Check first diagonal
if (theBoard[1][3] == 'X' && theBoard[2][2] == 'X'&& theBoard[3][1] == 'X'){
result = true;
System.out.println("The Player #1 (X) Wins!");
} else if (theBoard[1][3] == 'O' && theBoard[2][2] == 'O'&& theBoard[3][1] == 'O') {
result = true;
System.out.println("The Player #2 (O) Wins!");
}
return result;
}
}
附带说明一下,我是通过使用调试器并单步执行程序来发现这个错误的。
关于java - 井字游戏。工作不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31443604/
我正在维护一些 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
我是一名优秀的程序员,十分优秀!