玩家与计算机tic tac toe游戏代码

时间:2015-12-13 18:25:06

标签: java tic-tac-toe

我最近从我的AP Java类中分配了一个项目并遇到了一些困难。该项目的整个目标是创建一个玩家与计算机tic tac toe游戏。我已经成功创建了一个游戏板对象并在数组中输入了某些字符。但是我很困惑,不知道如何处理游戏的以下几个方面:

- 让玩家在玩家之后进行移动 - 让用户输入两个数字,允许他们进行移动而不是硬编码(我一直在使用随机值来测试) - 评估胜利或平局条件。

非常感谢!我最近对此代码感到非常沮丧,任何建议都会有所帮助!!

  • 董事会职业代码:

public class TicTacToeBoard{ private char[][] ttBoard;//initializes the type of array for the board private boolean gameActive = true;

public TicTacToeBoard(){ //Constructs our board as an object

ttBoard = new char[3][3]; //initialize the 2D array

for(int i = 0; i < ttBoard.length ; i++){
  Arrays.fill(ttBoard[i], ' '); 
}
}


public void printBoard(){ //we print our board object using this method
for(int i = 0; i<ttBoard.length; i++){
  for(int j = 0; j<ttBoard[i].length; j++){
    System.out.print(ttBoard[i][j]);
    if(j==0 || j==1)   //this loop helps separate indices, makes it look like a real board.
      System.out.print("|"); 
  }
  if(i==0||i==1)
  System.out.print("\n-----\n");
}
System.out.println();
}


  public boolean makeMove(char player , int i, int j)//here it prompts the player for a move 
//and also uses boolean to make the move
  {
   if(i >= 0 && i<= 2 && j >= 0 && j<= 2)
   {
      if(ttBoard[i][j] != ' ')
    return false;
   else
   {
    ttBoard[i][j] = player;
    return true;
   }
  } 
else
  return false;
}


public boolean gameOn()//this method will determine whether the game continues
{
 return gameActive
}

public void askPlayer(char player) //prompts users turn
{

}

public void askComp()  //prompts computers turn
{
}


}

主类代码:

public class TicTacToe{

public static void main(String[] args){
  TicTacToeBoard game = new TicTacToeBoard();
  game.printBoard();
  int count = 1;

while(game.gameOn())
{
  if (count%2 == 0)
    game.askPlayer('X');
  else
    game.askComp('O');
  count++;
}
game.printBoard();
}
}

2 个答案:

答案 0 :(得分:0)

你想要的第一个功能是这样吗? (不确定我真正得到你想要的东西)

public void askPlayer(char player) {
    Scanner sc = new Scanner(System.in);
    bool ok=false,ok2=false;
    int one, two;
    while(!ok2){//run while the input wanted is already taken
        while(!ok){//run while the input for the first coordinate isn't correct
            System.out.print("Enter first coordiante : ");
            try{
                one=sc.nextInt();
                if(one<1 || one>3)//in the array?
                    System.out.println("Wrong input");
                else
                    ok=true;
            }catch(InputMismatchException e){//a number is entered?
                System.out.println("Wrong input");
            }
        }
        ok=false;
        while(!ok){//same for second coordiante
            System.out.print("Enter second coordiante : ");
            try{
                two=sc.nextInt();
                if(two<1 || two>3)
                    System.out.println("Wrong input");
                else
                    ok=true;
            }catch(InputMismatchException e){
                System.out.println("Wrong input");
            }
        }   
        if(ttBoard[one-1][two-1]!=' ')//taken?
            System.out.println("Case already taken");
        else
            ok2=true;
    }
    ttBoard[one-1][two-1]=player;//affect players character
}

答案 1 :(得分:-1)

//for the Scanner
import java.util.*;

public class TicTacToe {
    private static boolean first = false;
    private static char[][] board = new char[3][3];
    private static int aiScore = 0;
    private static int playerScore = 0;


    public static void main(String[] args){
        intro();

    }

    public static void intro(){
        Scanner console = new Scanner(System.in);

        System.out.println();
        System.out.println();
        System.out.println("");
        System.out.println("Would you like to go first?");
        String goFirst = console.nextLine();

        if(goFirst.substring(0,1).equalsIgnoreCase("y")){
            first = true;
        }
        initializeBoard();
        getBoard();
        startGame(console);
    }


    public static void startGame(Scanner console){
        //clears the board to start the game
        if(first == true){
            userTurn(console);
        }else{
            compTurn(console);
        }
    }

    //allows user to play
    public static void userTurn(Scanner console){
        System.out.println("Where would you like to play?");

        int r = console.nextInt() - 1;
        int c = console.nextInt() - 1;

        if(board[r][c] == ' '){
            board[r][c] = 'X';

            if(board[0][0] == 'X'){
                getBoard();
                playerScore += 1;
                checkScoreUser(console);
            } else if(board[0][2] == 'X'){
                getBoard();
                playerScore += 3;
                checkScoreUser(console);
            } else if(board[2][0] == 'X'){
                getBoard();
                playerScore += 1000;
                checkScoreUser(console);
            } else if(board[2][2] == 'X'){
                getBoard();
                playerScore += 3000;
                checkScoreUser(console);
            } else if(board[1][1] == 'X'){
                getBoard();
                playerScore += 200;
                checkScoreUser(console);
            } else if(board[0][1] == 'X'){
                getBoard();
                playerScore += 2;
                checkScoreUser(console);
            } else if(board[1][0] == 'X'){
                getBoard();
                playerScore += 100;
                checkScoreUser(console);
            } else if(board[1][2] == 'X'){
                getBoard();
                playerScore += 300;
                checkScoreUser(console);
            } else if(board[2][1] == 'X'){
                getBoard();
                playerScore += 2000;
                checkScoreUser(console);
            }
        } else if (board[r][c] == 'X' || board[r][c] == 'O'){
            first = true;
            System.out.println("Please choose an empty space");
            startGame(console);
        }

        System.out.println("Cat's Game");
    }


    //allows computer to play
    public static void compTurn(Scanner console){

        System.out.println("Now it's my turn");
        System.out.println("");

        if(board[0][0] == ' '){
            board[0][0] = 'O';
            getBoard();
            userTurn(console);
            aiScore += 1;
        } else if(board[0][2] == ' '){
            board[0][2] = 'O';
            getBoard();
            userTurn(console);
            aiScore += 3;
        } else if(board[2][0] == ' '){
            board[2][0] = 'O';
            getBoard();
            userTurn(console);
            aiScore += 1000;
        } else if(board[2][2] == ' '){
            board[2][2] = 'O';
            getBoard();
            userTurn(console);
            aiScore += 3000;
        } else if(board[1][1] == ' '){
            board[1][1] = 'O';
            getBoard();
            userTurn(console);
            aiScore += 200;
        } else if(board[0][1] == ' '){
            board[0][1] = 'O';
            getBoard();
            userTurn(console);
            aiScore += 2;
        } else if(board[1][0] == ' '){
            board[1][0] = 'O';
            getBoard();
            userTurn(console);
            aiScore += 100;
        } else if(board[1][2] == ' '){
            board[1][2] = 'O';
            getBoard();
            userTurn(console);
            aiScore += 300;
        } else if(board[2][1] == ' '){
            board[2][1] = 'O';
            getBoard();
            userTurn(console);
            aiScore += 2000;
        }       
    }

    //score checkers
    public static void checkScoreUser(Scanner console){
        if(aiScore == 3201 || aiScore == 1203 || aiScore == 6 ||
           aiScore == 600 || aiScore == 6000 || aiScore == 1101 || 
           aiScore == 2202 || aiScore == 3303){
            System.out.println("I win!!! Hahahahahaha!!!!!!");
            intro();
        } else if (playerScore == 3201 || playerScore == 1203 || playerScore == 6 ||
                   playerScore == 600 || playerScore == 6000 || playerScore == 1101 || 
                   playerScore == 2202 || playerScore == 3303){
            System.out.println("You won!");
            intro();
        } else {
            compTurn(console);
        }
    }

    public static void checkScoreComp(Scanner console){
        if(aiScore == 3201 || aiScore == 1203 || aiScore == 6 ||
           aiScore == 600 || aiScore == 6000 || aiScore == 1101 || 
           aiScore == 2202 || aiScore == 3303){
            System.out.println("I win!!! Hahahahahaha!!!!!!");
            intro();
        } else if (playerScore == 3201 || playerScore == 1203 || playerScore == 6 ||
                   playerScore == 600 || playerScore == 6000 || playerScore == 1101 || 
                   playerScore == 2202 || playerScore == 3303){
            System.out.println("You won!");
            intro();
        } else {
            userTurn(console);
        }
    }
    //board stuff

    //initializes board and fills it with black spaces
    public static void initializeBoard(){
        for(int r = 0; r < 3; r++){
            for(int c = 0; c < 3; c++){
                board[r][c] = ' ';
            }
        }
    }

    //prints the actual board out
    public static void getBoard(){
        String bar = "  =============";
        System.out.println("    1   2   3");
        System.out.println(bar);
        System.out.println("1   " + board[0][0] + " | " + board[0][1] + " | " + board[0][2]);
        System.out.println(bar);
        System.out.println("2   " + board[1][0] + " | " + board[1][1] + " | " + board[1][2]);
        System.out.println(bar);
        System.out.println("3   " + board[2][0] + " | " + board[2][1] + " | " + board[2][2]);
        System.out.println(bar);
        System.out.println("");
    }
}