【问题标题】:Trouble getting TicTacToe game to run java无法让井字游戏运行 java
【发布时间】:2013-12-14 21:30:50
【问题描述】:

我的任务是创建一个井字游戏,让 X 可以与计算机对战。她希望玩家和计算机的移动是无效的,check 赢家是 int,打印和初始化板也是 int。我不认为我遇到了所有这些,但我只是希望它运行并实际输出正确。这也是我在comp sci 的第一年。在大多数情况下,我认为我的逻辑有效,它只是针对计算机移动方法,我对如何在 3x3 数组中获得随机移动感到困惑,所以这很可能没有意义。我的问题在于主要方法,尝试使用 do..while 循环来允许计算机显示它的移动。我的 machineTurn 方法应该是一个 void 方法,但错误表明它必须是一个 int。我不确定现在出了什么问题,但是我已经查看了这段代码一段时间了。我已经评论了很多。我的错误是:

java:58: 错误:不兼容的类型

move = machineTurn(theSeed);

必需:整数

发现:无效

1 个错误

过程完成。

import java.util.*;
public class TTT
{
public static final int EMPTY = 0;
public static final int COMPUTER = 2;
public static final int NONE = 0;
public static final int USER = 1;
public static final int theSeed = 0;


// Name-constants to represent the various states of the game
public static final int PLAYING = 0;
public static final int DRAW = 1;
public static final int USER_WON = 2;
public static final int COMPUTER_WON = 3;

// The game board and the game status
public static final int ROWS = 3, COLS = 3; // number of rows and columns
public static int[][] board = new int[ROWS][COLS]; // game board in 2D array
                                                  //  containing (EMPTY, CROSS, NOUGHT)
public static int currentState;  // the current state of the game
                                 // (PLAYING, DRAW, CROSS_WON, NOUGHT_WON)
public static int currentPlayer; // the current player (CROSS or NOUGHT)
public static int currentRow, currentCol; // current seed's row and column

public static Scanner in = new Scanner(System.in); // the input Scanner

public static void main(String[] args)
{
int move;
int winner;

initBoard();
printBoard();
// play the game once
do{
    yourTurn(currentPlayer);
    machineTurn(theSeed);
    updateGame(currentPlayer, currentRow, currentCol);
    printBoard();
    //print message if game-over
    if (currentState == USER_WON)
    {
        System.out.println(" You won! You beat the computer! Congratulations!");
    }
    else if ( currentState == COMPUTER_WON)
    {
        System.out.println(" You lost! The Computer Beat you! Sorry! ");
    }
    else if ( currentState == DRAW)
    {
        System.out.println(" No One won! It's a draw! ");
    }
    //switch player
    currentPlayer = (currentPlayer == USER) ? COMPUTER : USER;
    if( currentPlayer == COMPUTER)
    {
        move = machineTurn(theSeed);
        System.out.println("Computer move: " + move);
    }           
   }while( currentState == PLAYING); //repeat if not game over
 }

//initialize game board contents    
public static void initBoard()
{
    for (int row = 0; row < ROWS; ++row)
    {
     for (int col = 0; col < COLS; ++col) 
     {
        board[row][col] = EMPTY;  // all cells empty
     }
    }

}
public static void printBoard()
{
 for (int row = 0; row < ROWS; ++row) 
 {
   for (int col = 0; col < COLS; ++col) 
   {
    printCell(board[row][col]); // print each of the cells
     if (col != COLS - 1) 
     {
       System.out.print("|");   // print vertical partition
     }
   }
     System.out.println();
     if (row != ROWS - 1)
      {
        System.out.println("-----------"); // print horizontal partition
      }
  }
  System.out.println();
}
public static void printCell(int content) {
  switch (content) {
     case EMPTY:  System.out.print("   "); break;
     case COMPUTER: System.out.print(" O "); break;
     case USER:  System.out.print(" X "); break;
  }
}
public static boolean checkWinner(int theSeed, int currentRow,int currentCol)
{
  return (board[currentRow][0] == theSeed         // 3-in-the-row
               && board[currentRow][1] == theSeed
               && board[currentRow][2] == theSeed
          || board[0][currentCol] == theSeed      // 3-in-the-column
               && board[1][currentCol] == theSeed
               && board[2][currentCol] == theSeed
          || currentRow == currentCol            // 3-in-the-diagonal
               && board[0][0] == theSeed
               && board[1][1] == theSeed
               && board[2][2] == theSeed
          || currentRow + currentCol == 2  // 3-in-the-opposite-diagonal
               && board[0][2] == theSeed
               && board[1][1] == theSeed
               && board[2][0] == theSeed);
}
 public static boolean isDraw()
 {
    for (int row = 0; row < ROWS; row++)
    {
        for (int col = 0; col < COLS; col++)
        {
            if (board[row][col] == EMPTY)
            {
                return false; //an empty cell found, not draw, exit
            }
        }
    }
    return true; //no empty cell, it's draw
}
/* Player with the "theSeed" makes one move, with input validation.
   Update global variables "currentRow" and "currentCol". */
public static void yourTurn(int theSeed)
{
    boolean validInput = false;
    do {
     if (theSeed == USER)
      {
        System.out.print("Player 'X', enter your move (row[1-3] column[1-3]): ");
      }
     int row = in.nextInt() - 1;  // array index starts at 0 instead of 1
     int col = in.nextInt() - 1;
     if (row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] == EMPTY) {
        currentRow = row;
        currentCol = col;
        board[currentRow][currentCol] = theSeed;  // update game-board content
        validInput = true;  // input okay, exit loop
     } else {
        System.out.println("This move at (" + (row + 1) + "," + (col + 1)
              + ") is not valid. Try again...");
     }
  } while (!validInput);  // repeat until input is valid
}
/* supposed to be machine's random move after USER has gone */ 
public static void machineTurn(int theSeed)
{
    int move = (int)(Math.random()*9);
boolean validInput = false;
do{
    if(theSeed == COMPUTER);
    {
        board[(int)(move/3)][move%3] = currentPlayer;
    }


int row = in.nextInt() - 1;  // array index starts at 0 instead of 1
     int col = in.nextInt() - 1;
     if (row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] == EMPTY) {
        currentRow = row;
        currentCol = col;
        board[currentRow][currentCol] = theSeed;  // update game-board content
        }
       }while(validInput = true); // input okay, exit loop

}
public static void updateGame(int theSeed, int currentRow, int currentCol)
{
    if(checkWinner(theSeed, currentRow,currentCol))
    {
        currentState = (theSeed == USER) ? USER_WON : COMPUTER_WON;
    }
    else if (isDraw()) //check for draw
    {
        currentState = DRAW;
    }
 }
  //otherwise, no change to currentState (Still PLAYING)
}

此外,为了获得额外奖励,我们必须创建一个不允许玩家获胜的方法。这可能是平局,但电脑总是赢。谁能给我一个关于如何开始编写代码的提示?我将把那台机器命名为SmartMode。

【问题讨论】:

  • 编写不允许玩家获胜(只有平局/失败)的代码不是成为一名优秀程序员的问题,而是理解您正在玩的游戏的问题编程。学习井字游戏。
  • 你可以使用这个算法:xkcd.com/832
  • 错误信息告诉你问题出在什么地方。
  • 您在第 58 行的错误是由于您尝试使用返回不是 int 而是返回 void 的方法分配 int 移动。你可以直接说machineMove(theSeed),然后放弃move=
  • move = 表示编译器希望该行的其余部分结束计算值。但它只是调用了一个不返回值的方法(void 方法)。所以...

标签: java arrays methods multidimensional-array tic-tac-toe


【解决方案1】:

您的静态方法machineTurnvoid,但您正试图将结果分配给int。没有结果。

【讨论】:

  • 我应该如何更改它以使机器移动无效,例如 yourTurn 并且它仍然显示移动?
  • 您的代码中还有其他几个问题,所以这并没有太大帮助...但是将方法签名更改为 int 并返回机器从该方法的移动。跨度>
  • 介意告诉我我的其他问题是什么吗?我修复了所有这些,现在它只打印空板......我不知道我做错了什么......我以为我知道我在做什么......
  • 好吧,一方面if(theSeed == COMPUTER); if 结尾不应该有分号。
  • 哦!谢谢!我什至没有意识到……还有其他明显的错误吗?
【解决方案2】:

您声明变量move 类型为int 您的主要方法。然后您尝试启动machineTurn 方法并将其结果设置为move 变量。但是方法s result and variable type is incompatibe -voidcant 会显示为int。编译器这么说。

您应该更改方法的 machineTurn 签名以返回 int

public static int machineTurn(int theSeed)

不要忘记更改实现以返回您刚刚进行的移动的 int 值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多