4

所以这是一个大学作业,除了 AI 不知道如何“赢得胜利”,它的动作一开始并不像我想的那样随机,因为它们总是走上角。并努力下去。这项任务只需要人工智能的两条规则,即它可以“赢得胜利”或“阻止胜利”。

此外,当棋盘被填满时,游戏似乎并没有结束。它要求我选择我的行动。

我希望有些人可能对我如何改进它提出建议,或者指出我哪里出错了。

任何帮助将不胜感激。

这里是戏

import java.util.Scanner;

public class play {



    public static void main(String[] args) {


        System.out.println("Welcome to Tickle Tackle Toe!!! :D");
        System.out.println();

        //creat markers
        String marker1 = "x";
        String marker2 = "o";
        boolean playAgain = true;

        Scanner s = new Scanner(System.in);



        //create player objects
        Human human = new Human();
        Computer computer = new Computer();

        while(playAgain){
        //run board setup
        set Setup = new set();

        Setup.createBoard();
        Setup.printBoard();

        System.out.println("please choose your marker");
        System.out.println("type 1 for 'x' or 2 for 'o'");

        //set markers
        if(s.nextInt() == 1){
         // create player objects
         human.setMarker("x");
         computer.setMarker("o");
        } 
        else
        {
        human.setMarker("o");
        computer.setMarker("x");

        }




    // determine who goes first
    int first = (int) (Math.random() * 2);





    boolean won = false;
    int turns = 0;

   if(first == 0){
   System.out.println("You go first!");
    System.out.println();
   while(!won){
    human.takeTurn(Setup.getBoard());
    turns++;
    Setup.printBoard();
    if(Setup.hasWon(Setup.getBoard())){
        won = true;
        System.out.println("Congrats you won!");
        }
        if(turns == 9){
        won = true;
        System.out.println("Its a draw!");
        break;
        }
    if(!won){
        computer.takeTurn(Setup.getBoard(), human);
        turns++;
        System.out.println();
        Setup.printBoard();
        System.out.println();
        if(Setup.hasWon(Setup.getBoard())){
            won = true;
            System.out.println("You lost!");
            }
            if(turns == 9){
        won = true;
        System.out.println("Its a draw!");
        break;
        }
        }

   }  // close while 1
  }
  else {

    System.out.println("Computer goes first!");
    System.out.println();
    while(!won){
    computer.takeTurn(Setup.getBoard(), human);
    turns++;
    Setup.printBoard();
    if(Setup.hasWon(Setup.getBoard())){
        won = true;
        System.out.println("You lost!");
        }
    if(!won){
        human.takeTurn(Setup.getBoard());
        turns++;
        System.out.println();
        Setup.printBoard();
        System.out.println();
        if(Setup.hasWon(Setup.getBoard())){
            won = true;
            System.out.println("Congrats you won!");
            }
        }



   }  // close while 2

  }

  System.out.println("Would you like to play again? Type 1 for yes or 2 to quit");
   System.out.println();
   if(s.nextInt() == 2){
   playAgain = false;
   }

  }

}
}

这是计算机课

class Computer extends player {

    public Computer(){

    }

    int boardsize = 3;


    public void takeTurn(String[][] board, Human human) {

    int vertical = 0;
    int horizontal = 0;
    int diagonal = 0;
    boolean mademove = false;

    // check if you can take a win horizontally
    for(int i = 0; i<3; i++){

        if(board[0][i].equals(board[1][i]) && board[0][i].equals(Marker)){

            if(board[2][i] != human.getMarker()){
            board[2][i] = Marker;
            mademove = true;
            return;
            }

        }

    }

    for(int i = 0; i<3; i++){

        if(board[2][i].equals(board[1][i]) && board[2][i].equals(Marker)){

            if(board[0][i] != human.getMarker()){
            board[0][i] = Marker;
            mademove = true;
            return;
            }

        }


    }


        // check if you can take a win vertically
    for(int i = 0; i<3; i++){

        if(board[i][0].equals(board[i][1]) && board[i][0].equals(Marker)){

            if(board[i][2] != human.getMarker()){
            board[i][2] = Marker;
            mademove = true;
            return;
            }

        }

    }

    for(int i = 0; i<3; i++){

        if(board[i][2].equals(board[i][1]) && board[i][2].equals(Marker)){

            if(board[i][0] != human.getMarker()){
            board[i][0] = Marker;
            mademove = true;
            return;
            }

        }

    }


    // check if you can take a win diagonally 


        if(board[0][0].equals(board[1][1]) && board[0][0].equals(Marker)){

            if(board[2][2] != human.getMarker()){
            board[2][2] = Marker;
            mademove = true;
            return;
            }
    }

        if(board[2][2].equals(board[1][1]) && board[2][2].equals(Marker)){

            if(board[0][0] != human.getMarker()){
            board[0][0] = Marker;
            mademove = true;
            return;
            }
    }

        if(board[0][0].equals(board[1][1]) && board[0][0].equals(Marker)){

            if(board[2][2] != human.getMarker()){
            board[2][2] = Marker;
            mademove = true;
            return;
            }
    }

        if(board[0][2].equals(board[1][1]) && board[0][2].equals(Marker)){

            if(board[2][0] != human.getMarker()){
            board[2][0] = Marker;
            mademove = true;
            return;
            }
    }

        if(board[2][0].equals(board[1][1]) && board[2][0].equals(Marker)){

            if(board[0][2] != human.getMarker()){
            board[0][2] = Marker;
            mademove = true;
            return;
            }
    }


    // BLOCKS!!!! //

    // check if you can block a win horizontally
    for(int i = 0; i<3; i++){

        if(board[0][i].equals(board[1][i]) && board[0][i].equals(human.getMarker())){
            if(board[2][i] != Marker){
            board[2][i] = Marker;
            mademove = true;
            return;
            }

        }

    }

    for(int i = 0; i<3; i++){

        if(board[2][i].equals(board[1][i]) && board[0][i].equals(human.getMarker())){

            if(board[0][i] != Marker){
            board[0][i] = Marker;
            mademove = true;
            return;
            }

        }


    }



    // check if you can block a win horizontally
    for(int i = 0; i<3; i++){

        if(board[i][0].equals(board[i][1]) && board[i][0].equals(human.getMarker())){

            if(board[i][2] != Marker){
            board[i][2] = Marker;
            mademove = true;
            return;
            }

        }

    }

    for(int i = 0; i<3; i++){

        if(board[i][2].equals(board[i][1]) && board[i][2].equals(human.getMarker())){

            if(board[i][0] != Marker){
            board[i][0] = Marker;
            mademove = true;
            return;
            }

        }

    }


    // check if you can block a win diagonally 


        if(board[0][0].equals(board[1][1]) && board[0][0].equals(human.getMarker())){

            if(board[2][2] != Marker){
            board[2][2] = Marker;
            mademove = true;
            return;
            }
    }

        if(board[2][2].equals(board[1][1]) && board[2][2].equals(human.getMarker())){

            if(board[0][0] != Marker){
            board[0][0] = Marker;
            mademove = true;
            return;
            }
    }

        if(board[0][0].equals(board[1][1]) && board[0][0].equals(human.getMarker())){
            board[2][2] = Marker;
            mademove = true;
            return;
    }

        if(board[0][2].equals(board[1][1]) && board[0][2].equals(human.getMarker())){

            if(board[2][0] != Marker){
            board[2][0] = Marker;
            mademove = true;
            return;
            }
    }

        if(board[2][0].equals(board[1][1]) && board[2][0].equals(human.getMarker())){

            if(board[0][2] != Marker){
            board[0][2] = Marker;
            mademove = true;
            return;
            }
    }




         // make random move if above rules dont apply
        for(int i = 0; i<3; i++){
        if(board[i][0] != "x" && board[i][0] != "o"){
            board[i][0] = Marker;
            return;
            }
        }
        for(int i = 0; i<3; i++){
        if(board[i][1] != "x" && board[i][0] != "o"){
            board[i][1] = Marker;
            return;
            }
        }
            for(int i = 0; i<3; i++){
        if(board[i][2] != "x" && board[i][0] != "o"){
            board[i][2] = Marker;
            return;
            }
        }


    }   
}
4

4 回答 4

2

如果您通过放置第九个棋子获胜,您将同时获得“你赢了”和“这是平局!” 消息,我认为这不是预期的行为。如果你在第九回合输了也一样。另一方面,当没有人获胜时,没有达到抽奖代码,这就是游戏没有结束的原因。这很容易解决:

if(!won){
    computer.takeTurn(Setup.getBoard(), human);
    turns++;
    System.out.println();
    Setup.printBoard();
    System.out.println();
    if(Setup.hasWon(Setup.getBoard())){
        won = true;
        System.out.println("You lost!");
    } else if (turns == 9){
        won = true;
        System.out.println("Its a draw!");
    }
}

关于随机移动,您当前的代码循环遍历所有九个图块,在遇到的第一个空白点放置一个标记,然后返回。您想要的是列出所有空白点,然后随机选择其中一个。或者,继续生成 0 到 2 之间的 x 和 y 坐标,检查它是否为空,如果是则放置一个标记。

于 2012-05-19T12:15:55.027 回答
1

所以我想我会在找到我为此做的旧代码并回答我是如何做到的之后回来的。

它可能离完美还很远,但已经非常接近了。

我从一个非常简单的播放器类开始,它可以获取和设置标记。

public class player {

    public String Marker;

    // set marker method

    public void setMarker(String marker){

        Marker = marker;

    }

    //get marker method 

    public String getMarker(){

        return Marker;

    }

}

然后我添加了一个 set 类,它将在每次移动后设置和打印板。

import java.util.Random;
import java.util.Scanner;



public class set {

    // setup variables default board size and board
    private int N = 3;
    public String[][] board = new String [N][N];

    public boolean hasWon (String [] [] board){
        //horizontal 
        for(int i = 0; i<3; i++){
            if(board[i][0].equals(board[i][1]) && board[i][1].equals(board[i][2])){
                return true;
            }
        }
        //vertical
        for(int i = 0; i<3; i++){
            if(board [0][i].equals(board[1][i]) && board[1][i].equals(board[2][i])){
                return true;
            }
        }
        //diagonal
        if(board[0][0].equals(board[1][1]) && board[1][1].equals(board[2][2]) || board[2][0].equals(board[1][1]) && board[1][1].equals(board[0][2]))
            return true;
        return false;
    }


    int x = 1;

    public void createBoard(){
        for(int i = 0; i < board.length; i++){
            for(int j = 0; j < board.length; j++){
                board[i][j] = "" + (x);
                x++;
            }
        }
    }

    public void printBoard(){

        for(int i = 0; i < board.length; i++){
            for(int j = 0; j < board.length; j++){
                System.out.print("[" + board[i][j] + "]" + " ");

            }
            System.out.println();
        }

    }

    public String[][] getBoard(){

        return board;

    }



}

带有 AI 的计算机课程。

class Computer extends player {

public Computer(){

}

int boardsize = 3;


public void takeTurn(String[][] board, Human human) {

    int vertical = 0;
    int horizontal = 0;
    int diagonal = 0;
    boolean mademove = false;

    // check if you can take a win horizontally
    for(int i = 0; i<3; i++){

        if(board[0][i].equals(board[1][i]) && board[0][i].equals(Marker)){

            if(board[2][i] != human.getMarker() && board[2][i] != Marker){
                board[2][i] = Marker;
                mademove = true;
                return;
            }

        }

    }

    for(int i = 0; i<3; i++){

        if(board[2][i].equals(board[1][i]) && board[2][i].equals(Marker)){

            if(board[0][i] != human.getMarker() && board[0][i] != Marker){
                board[0][i] = Marker;
                mademove = true;
                return;
            }

        }


    }



    // check if you can take a win horizontally
    for(int i = 0; i<3; i++){

        if(board[i][0].equals(board[i][1]) && board[i][0].equals(Marker)){

            if(board[i][2] != human.getMarker() && board[i][2] != Marker){
                board[i][2] = Marker;
                mademove = true;
                return;
            }

        }

    }

    for(int i = 0; i<3; i++){

        if(board[i][2].equals(board[i][1]) && board[i][2].equals(Marker)){

            if(board[i][0] != human.getMarker() && board[i][0] != Marker){
                board[i][0] = Marker;
                mademove = true;
                return;
            }

        }

    }


    // check if you can take a win diagonally bottom


    if(board[0][0].equals(board[1][1]) && board[0][0].equals(Marker)){

        if(board[2][2] != human.getMarker() && board[2][2] != Marker){
            board[2][2] = Marker;
            mademove = true;
            return;
        }
    }

    if(board[2][2].equals(board[1][1]) && board[2][2].equals(Marker)){

        if(board[0][0] != human.getMarker() && board[0][0] != Marker){
            board[0][0] = Marker;
            mademove = true;
            return;
        }
    }

    if(board[0][0].equals(board[1][1]) && board[0][0].equals(Marker)){

        if(board[2][2] != human.getMarker() && board[2][2] != Marker){
            board[2][2] = Marker;
            mademove = true;
            return;
        }
    }

    if(board[0][2].equals(board[1][1]) && board[0][2].equals(Marker)){

        if(board[2][0] != human.getMarker() && board[2][0] != Marker){
            board[2][0] = Marker;
            mademove = true;
            return;
        }
    }

    if(board[2][0].equals(board[1][1]) && board[2][0].equals(Marker)){

        if(board[0][2] != human.getMarker() && board[0][2] != Marker){
            board[0][2] = Marker;
            mademove = true;
            return;
        }
    }


    // BLOCKS!!!! //

    // check if you can block a win horizontally
    for(int i = 0; i<3; i++){

        if(board[0][i].equals(board[1][i]) && board[0][i].equals(human.getMarker())){
            if(board[2][i] != Marker && board[2][i] != human.getMarker()){
                board[2][i] = Marker;
                mademove = true;
                return;
            }

        }

    }

    for(int i = 0; i<3; i++){

        if(board[2][i].equals(board[1][i]) && board[0][i].equals(human.getMarker())){

            if(board[0][i] != Marker && board[0][i] != human.getMarker()){
                board[0][i] = Marker;
                mademove = true;
                return;
            }

        }


    }

    // check if you can block a win vertically


    for(int i = 0; i<3; i++){

        if(board[i][0].equals(board[i][1]) && board[i][0].equals(human.getMarker())){

            if(board[i][2] != Marker && board[i][2] != human.getMarker()){
                board[i][2] = Marker;
                mademove = true;
                return;
            }

        }

    }

    for(int i = 0; i<3; i++){

        if(board[i][2].equals(board[i][1]) && board[i][2].equals(human.getMarker())){

            if(board[i][0] != Marker && board[i][0] != human.getMarker()){
                board[i][0] = Marker;
                mademove = true;
                return;
            }

        }

    }

    for(int i = 0; i<3; i++){

        if(board[2][i].equals(board[1][i]) && board[2][i].equals(human.getMarker())){

            if(board[0][i] != Marker && board[0][i] != human.getMarker()){
                board[0][i] = Marker;
                mademove = true;
                return;
            }

        }

    }



    // check if you can block a win diagonally 


    if(board[0][0].equals(board[1][1]) && board[0][0].equals(human.getMarker())){

        if(board[2][2] != Marker && board[2][2] != human.getMarker()){
            board[2][2] = Marker;
            mademove = true;
            return;
        }
    }

    if(board[2][2].equals(board[1][1]) && board[2][2].equals(human.getMarker())){

        if(board[0][0] != Marker && board[0][0] != human.getMarker()){
            board[0][0] = Marker;
            mademove = true;
            return;
        }
    }

    if(board[0][0].equals(board[1][1]) && board[0][0].equals(human.getMarker())){
        if(board[2][2] != Marker && board[2][2] != human.getMarker()){
            board[2][2] = Marker;
            mademove = true;
            return;
        }
    }

    if(board[0][2].equals(board[1][1]) && board[0][2].equals(human.getMarker())){

        if(board[2][0] != Marker && board[2][0] != human.getMarker()){
            board[2][0] = Marker;
            mademove = true;
            return;
        }
    }

    if(board[2][0].equals(board[1][1]) && board[2][0].equals(human.getMarker())){

        if(board[0][2] != Marker && board[0][2] != human.getMarker()){
            board[0][2] = Marker;
            mademove = true;
            return;
        }
    }




    // make random move if above rules dont apply
    int rand1 = 0;
    int rand2 = 0;

    while(!mademove){

        rand1 = (int) (Math.random() * 3);
        rand2 = (int) (Math.random() * 3);

        if(board[rand1][rand2] != "x" && board[rand1][rand2] != "o"){
            board[rand1][rand2] = Marker;
            mademove = true;        

        }

    }


}   

}

人类阶级

import java.util.Scanner;

类人类扩展玩家{

public Human(){

}


public void takeTurn(String[][] board) {

    Scanner s = new Scanner(System.in);

    boolean turn = true;

    while(turn){
        System.out.println("please enter row");
        int row = s.nextInt();
        System.out.println("please enter col");
        int col = s.nextInt();
        System.out.print("you entered "+row+" "+col);
        System.out.println();
        if(board[row - 1][col - 1] != "x" && board[row - 1][col - 1] != "o"){
            board[row - 1][col - 1] = Marker;
            turn = false;
        } // closes if
        else {
            System.out.println("Already Marker here! please guess again!");
        }
    } // ends while
} // ends method

} // ends class

终于开始玩游戏了。

    import java.util.Scanner;

public class play {



    public static void main(String[] args) {


        System.out.println("Welcome to Tickle Tackle Toe!!! :D");
        System.out.println();

        //creat markers
        String marker1 = "x";
        String marker2 = "o";
        boolean playAgain = true;

        Scanner s = new Scanner(System.in);



        //create player objects
        Human human = new Human();
        Computer computer = new Computer();

        while(playAgain){
            //run board setup
            set Setup = new set();

            Setup.createBoard();
            Setup.printBoard();

            System.out.println("please choose your marker");
            System.out.println("type 1 for 'x' or 2 for 'o'");

            //set markers
            if(s.nextInt() == 1){
                // create player objects
                human.setMarker("x");
                computer.setMarker("o");
            } 
            else
            {
                human.setMarker("o");
                computer.setMarker("x");

            }




            // determine who goes first
            int first = (int) (Math.random() * 2);





            boolean won = false;
            int turns = 0;

            if(first == 0){
                System.out.println("You gots the winz!");
                System.out.println();
                while(!won){
                    human.takeTurn(Setup.getBoard());
                    turns++;
                    Setup.printBoard();
                    if(Setup.hasWon(Setup.getBoard())){
                        won = true;
                        System.out.println("Congrats you won!");
                    }
                    if(turns == 9){
                        won = true;
                        System.out.println("Its a bore draw!");
                        break;
                    }
                    if(!won){
                        computer.takeTurn(Setup.getBoard(), human);
                        turns++;
                        System.out.println();
                        Setup.printBoard();
                        System.out.println();
                        if(Setup.hasWon(Setup.getBoard())){
                            won = true;
                            System.out.println("You just got pwned by an A.I with an incomplete rule set. FACEPALM.");
                        }
                        if(turns == 9){
                            won = true;
                            System.out.println("Its a bore draw!");
                            break;
                        }
                    }

                }  // close while 1
            }
            else {

                System.out.println("Computer goes first!");
                System.out.println();
                while(!won){
                    computer.takeTurn(Setup.getBoard(), human);
                    turns++;
                    Setup.printBoard();
                    if(Setup.hasWon(Setup.getBoard())){
                        won = true;
                        System.out.println("You just got pwned by an A.I with an incomplete rule set. FACEPALM.");
                    }
                    if(turns == 9){
                        won = true;
                        System.out.println("Its a draw!");
                        break;
                    }
                    if(!won){
                        human.takeTurn(Setup.getBoard());
                        turns++;
                        System.out.println();
                        Setup.printBoard();
                        System.out.println();
                        if(Setup.hasWon(Setup.getBoard())){
                            won = true;
                            System.out.println("You gots the winz!");
                        }
                        if(turns == 9){
                            won = true;
                            System.out.println("Its a draw!");
                            break;
                        }
                    }



                }  // close while 2

            }

            System.out.println("Would you like to play again? Type 1 for yes or 2 to quit");
            System.out.println();
            if(s.nextInt() == 2){
                playAgain = false;
            }

        }

    }
}
于 2013-04-17T11:02:30.450 回答
0

tttc 是我的 TicTacToeComputer 类的对象,
button[] 数组定义在 TicTacToeComputer,
TicTacToePlayer 是我的另一个具有静态 int 变量“count”的类,
TicTacToePlayer 具有三个布尔静态变量名称 winX、winY 和 win,

    static void move() // easiest way for move method for Artificial intelligence and study how its work and then you can use loops optimize for the code 
          {
            if ((tttc.button[0].getText().equals("O")) && (tttc.button[1].getText().equals("O")) && (tttc.button[2].getText().equals("")))
            {
              tttc.button[2].setText(TicTacToeComputer.letterO);
              tttc.button[2].setIcon(new ImageIcon("o.gif"));
              tttc.button[2].setEnabled(false);
            }
            else if ((tttc.button[3].getText().equals("O")) && (tttc.button[4].getText().equals("O")) && (tttc.button[5].getText().equals("")))
            {
              tttc.button[5].setText(TicTacToeComputer.letterO);
              tttc.button[5].setIcon(new ImageIcon("o.gif"));
              tttc.button[5].setEnabled(false);
            }
            else if ((tttc.button[6].getText().equals("O")) && (tttc.button[7].getText().equals("O")) && (tttc.button[8].getText().equals("")))
            {
              tttc.button[8].setText(TicTacToeComputer.letterO);
              tttc.button[8].setIcon(new ImageIcon("o.gif"));
              tttc.button[8].setEnabled(false);
            }
            else if ((tttc.button[1].getText().equals("O")) && (tttc.button[2].getText().equals("O")) && (tttc.button[0].getText().equals("")))
            {
              tttc.button[0].setText(TicTacToeComputer.letterO);
              tttc.button[0].setIcon(new ImageIcon("o.gif"));
              tttc.button[0].setEnabled(false);
            }
            else if ((tttc.button[4].getText().equals("O")) && (tttc.button[5].getText().equals("O")) && (tttc.button[3].getText().equals("")))
            {
              tttc.button[3].setText(TicTacToeComputer.letterO);
              tttc.button[3].setIcon(new ImageIcon("o.gif"));
              tttc.button[3].setEnabled(false);
            }
            else if ((tttc.button[7].getText().equals("O")) && (tttc.button[8].getText().equals("O")) && (tttc.button[6].getText().equals("")))
            {
              tttc.button[6].setText(TicTacToeComputer.letterO);
              tttc.button[6].setIcon(new ImageIcon("o.gif"));
              tttc.button[6].setEnabled(false);
            }
            else if ((tttc.button[0].getText().equals("O")) && (tttc.button[2].getText().equals("O")) && (tttc.button[1].getText().equals("")))
            {
              tttc.button[1].setText(TicTacToeComputer.letterO);
              tttc.button[1].setIcon(new ImageIcon("o.gif"));
              tttc.button[1].setEnabled(false);
            }
            else if ((tttc.button[3].getText().equals("O")) && (tttc.button[5].getText().equals("O")) && (tttc.button[4].getText().equals("")))
            {
              tttc.button[4].setText(TicTacToeComputer.letterO);
              tttc.button[4].setIcon(new ImageIcon("o.gif"));
              tttc.button[4].setEnabled(false);
            }
            else if ((tttc.button[6].getText().equals("O")) && (tttc.button[8].getText().equals("O")) && (tttc.button[7].getText().equals("")))
            {
              tttc.button[7].setText(TicTacToeComputer.letterO);
              tttc.button[7].setIcon(new ImageIcon("o.gif"));
              tttc.button[7].setEnabled(false);
            }
            else if ((tttc.button[0].getText().equals("O")) && (tttc.button[3].getText().equals("O")) && (tttc.button[6].getText().equals("")))
            {
              tttc.button[6].setText(TicTacToeComputer.letterO);
              tttc.button[6].setIcon(new ImageIcon("o.gif"));
              tttc.button[6].setEnabled(false);
            }
            else if ((tttc.button[1].getText().equals("O")) && (tttc.button[4].getText().equals("O")) && (tttc.button[7].getText().equals("")))
            {
              tttc.button[7].setText(TicTacToeComputer.letterO);
              tttc.button[7].setIcon(new ImageIcon("o.gif"));
              tttc.button[7].setEnabled(false);
            }
            else if ((tttc.button[2].getText().equals("O")) && (tttc.button[5].getText().equals("O")) && (tttc.button[8].getText().equals("")))
            {
              tttc.button[8].setText(TicTacToeComputer.letterO);
              tttc.button[8].setIcon(new ImageIcon("o.gif"));
              tttc.button[8].setEnabled(false);
            }
            else if ((tttc.button[3].getText().equals("O")) && (tttc.button[6].getText().equals("O")) && (tttc.button[0].getText().equals("")))
            {
              tttc.button[0].setText(TicTacToeComputer.letterO);
              tttc.button[0].setIcon(new ImageIcon("o.gif"));
              tttc.button[0].setEnabled(false);
            }
            else if ((tttc.button[4].getText().equals("O")) && (tttc.button[7].getText().equals("O")) && (tttc.button[1].getText().equals("")))
            {
              tttc.button[1].setText(TicTacToeComputer.letterO);
              tttc.button[1].setIcon(new ImageIcon("o.gif"));
              tttc.button[1].setEnabled(false);
            }
            else if ((tttc.button[5].getText().equals("O")) && (tttc.button[8].getText().equals("O")) && (tttc.button[2].getText().equals("")))
            {
              tttc.button[2].setText(TicTacToeComputer.letterO);
              tttc.button[2].setIcon(new ImageIcon("o.gif"));
              tttc.button[2].setEnabled(false);
            }
            else if ((tttc.button[0].getText().equals("O")) && (tttc.button[6].getText().equals("O")) && (tttc.button[3].getText().equals("")))
            {
              tttc.button[3].setText(TicTacToeComputer.letterO);
              tttc.button[3].setIcon(new ImageIcon("o.gif"));
              tttc.button[3].setEnabled(false);
            }
            else if ((tttc.button[1].getText().equals("O")) && (tttc.button[7].getText().equals("O")) && (tttc.button[4].getText().equals("")))
            {
              tttc.button[4].setText(TicTacToeComputer.letterO);
              tttc.button[4].setIcon(new ImageIcon("o.gif"));
              tttc.button[4].setEnabled(false);
            }
            else if ((tttc.button[2].getText().equals("O")) && (tttc.button[8].getText().equals("O")) && (tttc.button[5].getText().equals("")))
            {
              tttc.button[5].setText(TicTacToeComputer.letterO);

              tttc.button[5].setIcon(new ImageIcon("o.gif"));
              tttc.button[5].setEnabled(false);
            }
            else if ((tttc.button[0].getText().equals("O")) && (tttc.button[4].getText().equals("O")) && (tttc.button[8].getText().equals("")))
            {
              tttc.button[8].setText(TicTacToeComputer.letterO);
              tttc.button[8].setIcon(new ImageIcon("o.gif"));
              tttc.button[8].setEnabled(false);
            }
            else if ((tttc.button[4].getText().equals("O")) && (tttc.button[8].getText().equals("O")) && (tttc.button[0].getText().equals("")))
            {
              tttc.button[0].setText(TicTacToeComputer.letterO);
              tttc.button[0].setIcon(new ImageIcon("o.gif"));
              tttc.button[0].setEnabled(false);
            }
            else if ((tttc.button[0].getText().equals("O")) && (tttc.button[8].getText().equals("O")) && (tttc.button[4].getText().equals("")))
            {
              tttc.button[4].setText(TicTacToeComputer.letterO);
              tttc.button[4].setIcon(new ImageIcon("o.gif"));
              tttc.button[4].setEnabled(false);
            }
            else if ((tttc.button[2].getText().equals("O")) && (tttc.button[4].getText().equals("O")) && (tttc.button[6].getText().equals("")))
            {
              tttc.button[6].setText(TicTacToeComputer.letterO);
              tttc.button[6].setIcon(new ImageIcon("o.gif"));
              tttc.button[6].setEnabled(false);
            }
            else if ((tttc.button[6].getText().equals("O")) && (tttc.button[4].getText().equals("O")) && (tttc.button[2].getText().equals("")))
            {
              tttc.button[2].setText(TicTacToeComputer.letterO);
              tttc.button[2].setIcon(new ImageIcon("o.gif"));
              tttc.button[2].setEnabled(false);
            }
            else if ((tttc.button[6].getText().equals("O")) && (tttc.button[2].getText().equals("O")) && (tttc.button[4].getText().equals("")))
            {
              tttc.button[4].setText(TicTacToeComputer.letterO);
              tttc.button[4].setIcon(new ImageIcon("o.gif"));
              tttc.button[4].setEnabled(false);
            }
            if ((tttc.button[0].getText().equals("X")) && (tttc.button[1].getText().equals("X")) && (tttc.button[2].getText().equals("")))
            {
              tttc.button[2].setText(TicTacToeComputer.letterO);
              tttc.button[2].setIcon(new ImageIcon("o.gif"));
              tttc.button[2].setEnabled(false);
            }
            else if ((tttc.button[3].getText().equals("X")) && (tttc.button[4].getText().equals("X")) && (tttc.button[5].getText().equals("")))
            {
              tttc.button[5].setText(TicTacToeComputer.letterO);
              tttc.button[5].setIcon(new ImageIcon("o.gif"));
              tttc.button[5].setEnabled(false);
            }
            else if ((tttc.button[6].getText().equals("X")) && (tttc.button[7].getText().equals("X")) && (tttc.button[8].getText().equals("")))
            {
              tttc.button[8].setText(TicTacToeComputer.letterO);
              tttc.button[8].setIcon(new ImageIcon("o.gif"));
              tttc.button[8].setEnabled(false);
            }
            else if ((tttc.button[1].getText().equals("X")) && (tttc.button[2].getText().equals("X")) && (tttc.button[0].getText().equals("")))
            {
              tttc.button[0].setText(TicTacToeComputer.letterO);
              tttc.button[0].setIcon(new ImageIcon("o.gif"));
              tttc.button[0].setEnabled(false);
            }
            else if ((tttc.button[4].getText().equals("X")) && (tttc.button[5].getText().equals("X")) && (tttc.button[3].getText().equals("")))
            {
              tttc.button[3].setText(TicTacToeComputer.letterO);
              tttc.button[3].setIcon(new ImageIcon("o.gif"));
              tttc.button[3].setEnabled(false);
            }
            else if ((tttc.button[7].getText().equals("X")) && (tttc.button[8].getText().equals("X")) && (tttc.button[6].getText().equals("")))
            {
              tttc.button[6].setText(TicTacToeComputer.letterO);

              tttc.button[6].setIcon(new ImageIcon("o.gif"));
              tttc.button[6].setEnabled(false);
            }
            else if ((tttc.button[0].getText().equals("X")) && (tttc.button[2].getText().equals("X")) && (tttc.button[1].getText().equals("")))
            {
              tttc.button[1].setText(TicTacToeComputer.letterO);
              tttc.button[1].setIcon(new ImageIcon("o.gif"));
              tttc.button[1].setEnabled(false);
            }
            else if ((tttc.button[3].getText().equals("X")) && (tttc.button[5].getText().equals("X")) && (tttc.button[4].getText().equals("")))
            {
              tttc.button[4].setText(TicTacToeComputer.letterO);
              tttc.button[4].setIcon(new ImageIcon("o.gif"));
              tttc.button[4].setEnabled(false);
            }
            else if ((tttc.button[6].getText().equals("X")) && (tttc.button[8].getText().equals("X")) && (tttc.button[7].getText().equals("")))
            {
              tttc.button[7].setText(TicTacToeComputer.letterO);
              tttc.button[7].setIcon(new ImageIcon("o.gif"));
              tttc.button[7].setEnabled(false);
            }
            else if ((tttc.button[0].getText().equals("X")) && (tttc.button[3].getText().equals("X")) && (tttc.button[6].getText().equals("")))
            {
              tttc.button[6].setText(TicTacToeComputer.letterO);
              tttc.button[6].setIcon(new ImageIcon("o.gif"));
              tttc.button[6].setEnabled(false);
            }
            else if ((tttc.button[1].getText().equals("X")) && (tttc.button[4].getText().equals("X")) && (tttc.button[7].getText().equals("")))
            {
              tttc.button[7].setText(TicTacToeComputer.letterO);
              tttc.button[7].setIcon(new ImageIcon("o.gif"));
              tttc.button[7].setEnabled(false);
            }
            else if ((tttc.button[2].getText().equals("X")) && (tttc.button[5].getText().equals("X")) && (tttc.button[8].getText().equals("")))
            {
              tttc.button[8].setText(TicTacToeComputer.letterO);
              tttc.button[8].setIcon(new ImageIcon("o.gif"));
              tttc.button[8].setEnabled(false);
            }
            else if ((tttc.button[3].getText().equals("X")) && (tttc.button[6].getText().equals("X")) && (tttc.button[0].getText().equals("")))
            {
              tttc.button[0].setText(TicTacToeComputer.letterO);
              tttc.button[0].setIcon(new ImageIcon("o.gif"));
              tttc.button[0].setEnabled(false);
            }
            else if ((tttc.button[4].getText().equals("X")) && (tttc.button[7].getText().equals("X")) && (tttc.button[1].getText().equals("")))
            {
              tttc.button[1].setText(TicTacToeComputer.letterO);
              tttc.button[1].setIcon(new ImageIcon("o.gif"));
              tttc.button[1].setEnabled(false);
            }
            else if ((tttc.button[5].getText().equals("X")) && (tttc.button[8].getText().equals("X")) && (tttc.button[2].getText().equals("")))
            {
              tttc.button[2].setText(TicTacToeComputer.letterO);
              tttc.button[2].setIcon(new ImageIcon("o.gif"));
              tttc.button[2].setEnabled(false);
            }
            else if ((tttc.button[0].getText().equals("X")) && (tttc.button[6].getText().equals("X")) && (tttc.button[3].getText().equals("")))
            {
              tttc.button[3].setText(TicTacToeComputer.letterO);
              tttc.button[3].setIcon(new ImageIcon("o.gif"));
              tttc.button[3].setEnabled(false);
            }
            else if ((tttc.button[1].getText().equals("X")) && (tttc.button[7].getText().equals("X")) && (tttc.button[4].getText().equals("")))
            {
              tttc.button[4].setText(TicTacToeComputer.letterO);
              tttc.button[4].setIcon(new ImageIcon("o.gif"));
              tttc.button[4].setEnabled(false);
            }
            else if ((tttc.button[2].getText().equals("X")) && (tttc.button[8].getText().equals("X")) && (tttc.button[5].getText().equals("")))
            {
              tttc.button[5].setText(TicTacToeComputer.letterO);
              tttc.button[5].setIcon(new ImageIcon("o.gif"));
              tttc.button[5].setEnabled(false);
            }
            else if ((tttc.button[0].getText().equals("X")) && (tttc.button[4].getText().equals("X")) && (tttc.button[8].getText().equals("")))
            {
              tttc.button[8].setText(TicTacToeComputer.letterO);
              tttc.button[8].setIcon(new ImageIcon("o.gif"));
              tttc.button[8].setEnabled(false);
            }
            else if ((tttc.button[4].getText().equals("X")) && (tttc.button[8].getText().equals("X")) && (tttc.button[0].getText().equals("")))
            {
              tttc.button[0].setText(TicTacToeComputer.letterO);
              tttc.button[0].setIcon(new ImageIcon("o.gif"));
              tttc.button[0].setEnabled(false);
            }
            else if ((tttc.button[0].getText().equals("X")) && (tttc.button[8].getText().equals("X")) && (tttc.button[4].getText().equals("")))
            {
              tttc.button[4].setText(TicTacToeComputer.letterO);
              tttc.button[4].setIcon(new ImageIcon("o.gif"));
              tttc.button[4].setEnabled(false);
            }
            else if ((tttc.button[2].getText().equals("X")) && (tttc.button[4].getText().equals("X")) && (tttc.button[6].getText().equals("")))
            {
              tttc.button[6].setText(TicTacToeComputer.letterO);

              tttc.button[6].setIcon(new ImageIcon("o.gif"));
              tttc.button[6].setEnabled(false);
            }
            else if ((tttc.button[6].getText().equals("X")) && (tttc.button[4].getText().equals("X")) && (tttc.button[2].getText().equals("")))
            {
              tttc.button[2].setText(TicTacToeComputer.letterO);
              tttc.button[2].setIcon(new ImageIcon("o.gif"));
              tttc.button[2].setEnabled(false);
            }
            else if ((tttc.button[6].getText().equals("X")) && (tttc.button[2].getText().equals("X")) && (tttc.button[4].getText().equals("")))
            {
              tttc.button[4].setText(TicTacToeComputer.letterO);
              tttc.button[4].setIcon(new ImageIcon("o.gif"));
              tttc.button[4].setEnabled(false);
            }
            else if ((tttc.button[0].getText().equals("X")) && (tttc.button[4].getText().equals("O")) && (tttc.button[8].getText().equals("X")))
            {
              tttc.button[5].setText(TicTacToeComputer.letterO);
              tttc.button[5].setIcon(new ImageIcon("o.gif"));
              tttc.button[5].setEnabled(false);
            }
            else if ((tttc.button[2].getText().equals("X")) && (tttc.button[4].getText().equals("O")) && (tttc.button[6].getText().equals("X")))
            {
              tttc.button[3].setText(TicTacToeComputer.letterO);
              tttc.button[3].setIcon(new ImageIcon("o.gif"));
              tttc.button[3].setEnabled(false);
            }
            else if (tttc.button[4].getText().equals(""))
            {
              tttc.button[4].setText(TicTacToeComputer.letterO);
              tttc.button[4].setIcon(new ImageIcon("o.gif"));
              tttc.button[4].setEnabled(false);
            }
            else if (tttc.button[0].getText().equals(""))
            {
              tttc.button[0].setText(TicTacToeComputer.letterO);
              tttc.button[0].setIcon(new ImageIcon("o.gif"));
              tttc.button[0].setEnabled(false);
            }
            else
            {
              random();
            }
          }       




            static void random() // random method so that player and computer both have chance to win
                  {
                    if (TicTacToePlayer.count < 9)
                    {
                      int i = 0;
                      while (i == 0) {
                        i = (int)(Math.random() * 9.0D);
                      }
                      if (doRandomMove(tttc.button[i]))
                      {
                        tttc.button[i].setText(TicTacToeComputer.letterO);
                        tttc.button[i].setIcon(new ImageIcon("o.gif"));
                        tttc.button[i].setEnabled(false);
                      }
                      else
                      {
                        random();
                      }
                    }
                  }

                  public static boolean doRandomMove(JButton paramJButton)
                  {
                    if ((paramJButton.getText().equals("O")) || (paramJButton.getText().equals("X"))) {
                      return false;
                    }
                    return true;
                  }

    static void checkComputerWin()
      {
        if ((tttc.button[0].getText() == "X") && (tttc.button[1].getText() == "X") && (tttc.button[2].getText() == "X"))
        {
          TicTacToePlayer.winX = true;
        }
        else if ((tttc.button[3].getText() == "X") && (tttc.button[4].getText() == "X") && (tttc.button[5].getText() == "X"))
        {
          TicTacToePlayer.winX = true;
        }
        else if ((tttc.button[6].getText() == "X") && (tttc.button[7].getText() == "X") && (tttc.button[8].getText() == "X"))
        {
          TicTacToePlayer.winX = true;
        }
        else if ((tttc.button[0].getText() == "X") && (tttc.button[3].getText() == "X") && (tttc.button[6].getText() == "X"))
        {
          TicTacToePlayer.winX = true;
        }
        else if ((tttc.button[1].getText() == "X") && (tttc.button[4].getText() == "X") && (tttc.button[7].getText() == "X"))
        {
          TicTacToePlayer.winX = true;
        }
        else if ((tttc.button[2].getText() == "X") && (tttc.button[5].getText() == "X") && (tttc.button[8].getText() == "X"))
        {
          TicTacToePlayer.winX = true;
        }
        else if ((tttc.button[0].getText() == "X") && (tttc.button[4].getText() == "X") && (tttc.button[8].getText() == "X"))
        {
          TicTacToePlayer.winX = true;
        }
        else if ((tttc.button[2].getText() == "X") && (tttc.button[4].getText() == "X") && (tttc.button[6].getText() == "X"))
        {
          TicTacToePlayer.winX = true;
        }
        else if ((tttc.button[0].getText() == "O") && (tttc.button[1].getText() == "O") && (tttc.button[2].getText() == "O"))
        {
          TicTacToePlayer.winO = true;
        }
        else if ((tttc.button[3].getText() == "O") && (tttc.button[4].getText() == "O") && (tttc.button[5].getText() == "O"))
        {
          TicTacToePlayer.winO = true;
        }
        else if ((tttc.button[6].getText() == "O") && (tttc.button[7].getText() == "O") && (tttc.button[8].getText() == "O"))
        {
          TicTacToePlayer.winO = true;
        }
        else if ((tttc.button[0].getText() == "O") && (tttc.button[3].getText() == "O") && (tttc.button[6].getText() == "O"))
        {
          TicTacToePlayer.winO = true;
        }
        else if ((tttc.button[1].getText() == "O") && (tttc.button[4].getText() == "O") && (tttc.button[7].getText() == "O"))
        {
          TicTacToePlayer.winO = true;
        }
        else if ((tttc.button[2].getText() == "O") && (tttc.button[5].getText() == "O") && (tttc.button[8].getText() == "O"))
        {
          TicTacToePlayer.winO = true;
        }
        else if ((tttc.button[0].getText() == "O") && (tttc.button[4].getText() == "O") && (tttc.button[8].getText() == "O"))
        {
          TicTacToePlayer.winO = true;
        }
        else if ((tttc.button[2].getText() == "O") && (tttc.button[4].getText() == "O") && (tttc.button[6].getText() == "O"))
        {
          TicTacToePlayer.winO = true;
        }
        else
        {
          TicTacToePlayer.win = false;
          TicTacToePlayer.count += 1;
        }
        if ((!TicTacToePlayer.win) && (TicTacToePlayer.count > 8))
        {
          ImageIcon localImageIcon1 = new ImageIcon("cry.gif");
          JOptionPane.showMessageDialog(null, "The game was tie!", "Tie Game", 0, localImageIcon1);
          ImageIcon localImageIcon2 = new ImageIcon("newgame.gif");
          int i = JOptionPane.showConfirmDialog(null, "Start", "Do you want to start a New Game ?", 0, 0, localImageIcon2);
          if (i == 0)
          {
            TicTacToePlayer.count = 0;
            tttc.setVisible(false);
            new TicTacToeComputer("Wanna Play TicTacToe...!!!");
          }
          else
          {
            ImageIcon localImageIcon3 = new ImageIcon("bye.gif");
            JOptionPane.showMessageDialog(null, "Thank you " + TicTacToePlayer.x + " and " + TicTacToePlayer.z + " for playing", "Thank You", 1, localImageIcon3);
            System.exit(0);
          }
        }
        if (TicTacToePlayer.winX == true)
        {
          TicTacToePlayer.count = 0;
          tttc.setVisible(false);
          new AnimationComputerX();
          TicTacToePlayer.winX = false;
        }
        if (TicTacToePlayer.winO == true)
        {
          TicTacToePlayer.count = 0;
          tttc.setVisible(false);
          new AnimationComputerO();
          TicTacToePlayer.winO = false;
        }
      }

快乐编码 :) RChadZ

于 2014-05-23T13:24:15.270 回答
-1
//diagonal
if(board[0][0].equals(board[1][1]) && board[1][1].equals(board[2][2]) || board[2][0].equals(board[1][1]) && board[1][1].equals(board[0][2]))
       { return true;} // <<<<<you forgot the {} ^_^
    return false;
}
于 2013-12-06T02:39:50.040 回答