Pseudo Code for TicTacToe AI

posted on 12 Jul 2009 20:52 by dirofblue in Programming, Software

 

tictactoe pic

 

ส่วนใหญ่ก็ comment ไว้แล้วอะนะ อันนี้คิดเองแบบมั่วๆ 555+  ผิดก็ตัวใครตัวมันละเด้อ

import library
public class TicTacToeAI extends JFrame implements ActionListener{
    private Vector vAI = new Vector();
    private Vector vUser = new Vector();
    private Vector win = new Vector();
    private boolean isOver = false;
    private int turn = 0;
    private int[][] winCombination = new int[][] { //Combination for win the game
            {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, //horizontal wins
            {1, 4, 7}, {2, 5, 8}, {3, 6, 9}, //vertical wins
            {1, 5, 9}, {3, 5, 7}    //diagonal wins
    };
    public static void main(String[] args){
        TicTacToeAI tic = new TicTacToeAI();
    }
    public void AI(){
        if(!isOver){
            if(turn<10){
                if(!canWin()){ //First priority is to win!!
                    blockUser(); //If can't win, don't let user win.
                }
            }
            else{
                gameOver("NO"); //All cell use but no one win.
            }
        }
        turn++;
    }
    boolean canWin(){ //Check if AI can win
        if(still can win){
            aIMove(cell);
            return true
        }
        else
            return false;
    }
    void blockUser(){ //Try to block user from winning
        if(can move to block user){
            aIMove(cell);
        }
        else //If there's no cell to block then any cell will do.
            randomMove();
    }
    void randomMove(){
        //Random move when can't do anything.
    }
    void aIMove(cell position){
        //Move according to AI decision.
    }
    boolean checkWinCondition(String user){
        if(user){ // Check User Table
            if(vUser.contains(any pattern in win)){
                return true
            }
            else
                return false;
        }
        else{ // Check AI Table
            if(vAI.contains(any pattern in win)){
                return true
            }
            else
                return false;
        }
    }
    public void newDraw(String user,int position){
        //Add user action to table
    }
    public void gameOver(String user){
        if(user == "NO"){
            //Draw
        }
        //announce the winner.
    }
    public TicTacToeAI(){
        createGUI();
        for(int i=0;i
            for(int j=0;j
                win.add(wins[i][j]);
            }
        }
    }
    public void actionPerformed(ActionEvent e){
        if(buttonClicked){
            String user = checkUser();
            if(It is user turn)
            newDraw(user,position);
            if(!checkWinCondition(user))
            AI();
        }
    }
}
 

Comment

Comment:

Tweet

Superb internet site you've here.

my weblog: servers

#4 By (37.239.46.18|37.239.46.18) on 2014-05-29 07:32

Excellent & remarkable post!
I have been visiting various blogs for my Term Papers writing research.I have found your blog to be quite useful. Keep updating your blog with valuable information....... Regards

#3 By Term Papers writing (182.178.31.92) on 2010-10-11 18:53

The nice article sites just found your site on with yahoo helped me a lot! I really like the info, thank you.

#2 By เกมส์ (124.122.238.91) on 2010-09-29 11:04

จะเทพ ไปไหนอ่ะ ?

#1 By PolarHoney on 2009-07-12 22:43