Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Level Hard on Tic Tac Toe
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
programmer1337




PostPosted: Wed Jun 13, 2012 10:03 pm   Post subject: Level Hard on Tic Tac Toe

I am making a Tic Tac Toe game, now I have the easy, and medium done, all I need is the hard, here is my code at the moment
code:
public static void hard(String phrase) {
        int winner = 0;
        board[1][1] = " X ";
        int c = 2;
        phrase = getPhrase();
        while (winner == 0) {
            if (c % 2 == 0) {
                rowColumn(phrase, "1", 2);
            } else {
                System.out.println("hello");

                if (board[0][0].equals(" - ")) {
                    board[0][0] = " X ";

                } else if (!board[0][0].equals(" - ") && board[0][2].equals(" - ")) {
                    board[0][2] = " X ";
                } else if (!board[0][0].equals(" - ") && !board[0][2].equals(" - ") && board[2][0].equals(" - ")) {
                    board[2][0] = " X ";
                } else if (!board[0][0].equals(" - ") && !board[0][2].equals(" - ") && !board[2][0].equals(" - ") && board[2][2].equals(" - ")) {
                    board[2][2] = " X ";
                }
            }
            phrase = getPhrase();
            winner = determineWin();
            c++;
        }
        if (winner == 2) {
            System.out.println("you win");
        } else if (winner == 1) {
            System.out.println("cpu wins");
        }
    }
Now I dont want help with coding or anything, but the code above will put an X according to which corners are open, and I wouldnt know which part of the if statement I would need to put another if statment to check if there is a spot open for a win, like for example if you have(below), which part of the if statement would you make the computer put an x where it
would win
Quote:
X - X
- X O
- - O
Sponsor
Sponsor
Sponsor
sponsor
programmer1337




PostPosted: Wed Jun 13, 2012 11:18 pm   Post subject: RE:Level Hard on Tic Tac Toe

code:
   public static void hard(String phrase) {
        int winner = 0;
        board[1][1] = " X ";
        int c = 2;
        int d = 0;
        int xVal = 0;
        phrase = getPhrase();
        while (winner == 0) {
            if (c % 2 == 0) {
                rowColumn(phrase, "1", 2);
            } else {
                if (!board[0][0].equals(" - ") && !board[0][2].equals(" - ") && !board[2][0].equals(" - ") && !board[2][2].equals(" - ")) {
                    if (board[0][0].equals(" X ") && board[0][2].equals(" X ")) {
                        board[0][1] = " X ";
                    } else if (board[0][0].equals(" X ") && board[2][0].equals(" X ")) {
                        board[1][0] = " X ";
                    } else if (board[2][2].equals(" X ") && board[0][2].equals(" X ")) {
                        board[1][2] = " X ";
                    } else if (board[2][2].equals(" X ") && board[2][0].equals(" X ")) {
                        board[2][1] = " X ";
                    }
                }
                if (board[0][0].equals(" - ")) {
                    board[0][0] = " X ";

                } else if (!board[0][0].equals(" - ") && board[0][2].equals(" - ")) {
                    board[0][2] = " X ";
                } else if (!board[0][0].equals(" - ") && !board[0][2].equals(" - ") && board[2][0].equals(" - ")) {
                    board[2][0] = " X ";
                } else if (!board[0][0].equals(" - ") && !board[0][2].equals(" - ") && !board[2][0].equals(" - ") && board[2][2].equals(" - ")) {
                    board[2][2] = " X ";
                }

            }
            phrase = getPhrase();
            winner = determineWin();
            c++;
        }
        if (winner == 2) {
            JOptionPane.showMessageDialog(null, "You Win");
        } else if (winner == 1) {
            JOptionPane.showMessageDialog(null, "CPU Wins");
        }
    }
My new code, still not unbeatable
Tony




PostPosted: Thu Jun 14, 2012 1:32 am   Post subject: RE:Level Hard on Tic Tac Toe

Tic Tac Toe is a solved game. For any board, there exists an optimal move. All such moves are here -- http://xkcd.com/832/
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: