2013-12-14 3 views
0

과제는 컴퓨터에서 X를 플레이 할 수있는 tic tac toe 게임을 만드는 것이 었습니다. 그녀는 플레이어와 컴퓨터의 움직임을 무효화하고, 수표 우승자를 int로, 인쇄물과 초기화 보드를 int로 만들길 원했습니다. 나는 그들 모두를 만난 적이 있다고 생각하지는 않지만, 나는 그것을 실행하고 실제로 결과물을 출력하기를 원한다. 이것은 comp sci의 첫해입니다. 대부분의 경우, 내 논리가 작동한다고 생각합니다. 컴퓨터 이동 방법 일뿐입니다. 3x3 배열에서 무작위 이동을 얻는 방법에 대해서는 혼란스러워서별로 의미가 없습니다. 내 문제는 컴퓨터가 이동을 표시 할 수 있도록 do..while 루프를 사용하려고하는 주된 방법입니다. 내 machineTurn 메쏘드는 void 메쏘드라고 가정하고 있지만, 오류는 int이어야한다고 말한다. 지금 무엇이 잘못되었는지는 모르겠지만이 코드를 잠깐 살펴 봤습니다. 나는 그것의 좋은 조금을 논평했다. 내 오류는 다음과 같습니다TicTacToe 게임에서 자바를 실행하는 데 문제가 발생했습니다.

자바 : 58 : 오류 : 호환되지 않는 유형의

이동 = machineTurn (theSeed를); 필요한

는 :

프로세스가 완료 무효

1 오류 : 발견

을 int로.

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

또한 크레디트를 얻으려면 플레이어가 이기지 못하도록하는 방법을 만들어야합니다. 그것은 무승부가 될 수 있지만, 컴퓨터는 항상 승리합니다. 누구든지 코드 작성을 시작하는 방법에 대한 힌트를 줄 수 있습니까? 나는 그 기계의 스마트 모드라고 부를 것이다.

+0

만든 이동의 int 값을 반환하도록 구현을 변경하는 것을 잊지 마세요 (무승부/상실)을이기는 것은 프로그래밍중인 게임을 이해하는 문제만큼 좋은 프로그래머가되는 문제는 아닙니다. tic-tac-toe에 대해 공부하십시오. – nhgrif

+1

다음 알고리즘을 사용할 수 있습니다. http://xkcd.com/832/ – SJuan76

+0

오류 메시지는 문제의 위치와 위치를 정확히 알려줍니다. – Kevin

답변

0

정적 메서드 machineTurnvoid이지만 결과를 int에 할당하려고합니다. 결과가 없습니다.

+0

기계의 이동이 무효화되도록 yourTurn과 같이 변경해야하며 이동을 계속 표시합니까? – AJ114

+0

코드에 몇 가지 다른 이슈가 있으므로, 이것은별로 도움이되지 않지만 메소드 시그니처를'int'로 변경하고 메소드에서 기계의 이동을 리턴합니다. –

+0

내 다른 문제는 무엇입니까? 나는 그 모든 것을 고쳐서 이제는 단지 빈 보드만을 프린트한다 ... 나는 내가 뭘 잘못했는지 모른다 ... 나는 내가하고있는 것을 알고 있다고 생각했다 ... – AJ114

0

변수 move을 주 방법 인 int으로 선언하십시오. 그런 다음 machineTurn 메소드를 실행하고 그 결과를 move 변수로 설정하려고합니다. 그러나 방법 s result and variable type is incompatibe -canint으로 표시해야합니다. 편집자는 그렇게 말합니다.

당신은 반환 method`s machineTurn 서명을 변경해야합니다 INT

public static int machineTurn(int theSeed) 

방금 ​​플레이어를 허용하지 않는 코드 작성

관련 문제