无法让TicTacToe游戏运行java

时间:2013-12-14 21:30:51

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

我的任务是创建一个允许X与计算机对战的tic tac toe游戏。她希望播放器和计算机移动无效,检查获胜者为int,print和init板也是int。我不认为我遇到了所有这些,但我只想让它运行并实际输出正确。这是我在comp sci的第一年。在大多数情况下,我认为我的逻辑是有效的,它仅适用于计算机移动方法,我对如何在3x3阵列中获得随机移动感到困惑,因此可能没有任何意义。我的问题是在main方法中,尝试使用do..while循环来允许计算机显示它的移动。我的machineTurn方法应该是一个void方法,但错误说它必须是一个int。我不确定现在有什么问题但是,我一直在看这段代码。我评论了很多。我的错误是:

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

move = machineTurn(theSeed);

必需:int

发现:无效

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)
}

此外,为了额外的功劳,我们必须创建一个不允许玩家获胜的方法。它可以是一个平局,但计算机总是赢。任何人都可以给我一个关于如何开始编码的提示吗?我将其命名为machineSmartMode。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您使用主要方法类型move声明变量int。然后,您尝试启动machineTurn方法并将其结果设置为move变量。但方法s result and variable type is incompatibe - void can表示为int。 Compilator这样说。

您应该更改method`s machineTurn签名以返回int

public static int machineTurn(int theSeed)

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