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

Username:   Password: 
 RegisterRegister   
 Help With TICTACTOE
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
programmer1337




PostPosted: Mon Jun 11, 2012 9:53 pm   Post subject: Help With TICTACTOE

Ok so I have my two player tic tac toe working, now I need my easy option to work against cpu, for easy i made it a random number generator, now after 3 or 4 moves the input box just quits and the program stops without checking for a win...here is my code
code:
package tictac;

import javax.swing.*;

/**
 *
 * @author DANIEL
 */
public class Tictac {

    static String[][] board = new String[3][3];

    public static void main(String[] args) {
        String sChoice;
        int choices = 0;
        int choice = displayMenu();
        String phrase = "";

        for (int i = 0; i < 3; i++) {
            for (int x = 0; x < 3; x++) {
                board[i][x] = " - ";
                phrase += board[i][x];
            }
            phrase += "\n";
        }
        if (choice == 1) {
            twoPlayers(phrase);
        } else if (choice == 2) {
            while (choices > 2 || choices < 1) {
                sChoice = JOptionPane.showInputDialog("Difficulty \n"
                        + "1.  Easy \n"
                        + "2.  Hard");
                choices = Integer.parseInt(sChoice);
            }
            if (choices == 1) {
                easy(phrase);
            } else if (choices == 2) {
                hard(phrase);
            }
        } else if (choice == 3) {
            System.exit(0);
        }
    }

    public static void easy(String phrase) {
        String sPlay, sPlay1;
        int play1 = 0;
        int winner = 0;
        int computerRandom = (int) (Math.random() * 2) + 0;;
        int computerRandom1 = (int) (Math.random() * 2) + 0;;
        int play = 0;
        int c = 2;
        while (winner == 0) {
            if (c % 2 == 0) {
                sPlay = JOptionPane.showInputDialog(phrase + "\nPlayer 1 enter a row");
                sPlay1 = JOptionPane.showInputDialog(phrase + "\nPlayer 1 enter a column");
                play = Integer.parseInt(sPlay);
                play1 = Integer.parseInt(sPlay1);
                while (!board[play - 1][play1 - 1].equals(" - ")) {
                    JOptionPane.showMessageDialog(null, "Position has beem taken!");
                    sPlay = JOptionPane.showInputDialog(phrase + "\nPlayer 1 enter a row");
                    sPlay1 = JOptionPane.showInputDialog(phrase + "\nPlayer 1 enter a column");
                    play = Integer.parseInt(sPlay);
                    play1 = Integer.parseInt(sPlay1);
                }
                board[play - 1][play1 - 1] = " X ";
            } else {
                while (!board[computerRandom][computerRandom1].equals(" - ")) {
                    computerRandom = (int) (Math.random() * 2) + 0;
                    computerRandom1 = (int) (Math.random() * 2) + 0;
                }
                board[computerRandom][computerRandom1] = " O ";
            }
            phrase = "";
            for (int i = 0; i < 3; i++) {
                for (int x = 0; x < 3; x++) {
                    phrase += board[i][x];
                }
                phrase += "\n";
            }
            c += 1;
            winner = determineWin();
            System.out.println(winner);
        }
        if (winner == 1) {
            JOptionPane.showMessageDialog(null, "Player 1 winner!");
        } else if (winner == 2) {
            JOptionPane.showMessageDialog(null, "CPU is WINNER!");
        }
    }

    public static void hard(String phrase) {
    }

    public static int determineWin() {
        int counter = 0;
        int counter2 = 0;
        int winner = 0;
        int c = 2;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[i][j].equals(" X ")) {
                    counter += 1;
                }
                if (board[i][j].equals(" O ")) {
                    counter2 += 1;
                }
            }
            if (counter == 3) {
                winner = 1;
            } else if (counter2 == 3) {
                winner = 2;
            }
            counter = 0;
            counter2 = 0;
        }
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[j][i].equals(" X ")) {
                    counter += 1;
                }
                if (board[j][i].equals(" O ")) {
                    counter2 += 1;
                }
            }
            if (counter == 3) {
                winner = 1;
            } else if (counter2 == 3) {
                winner = 2;
            }
            counter = 0;
            counter2 = 0;
        }
        for (int i = 0; i < 3; i++) {
            if (board[i][i].equals(" X ")) {
                counter += 1;
            } else if (board[i][i].equals(" O ")) {
                counter2 += 1;
            }
        }
        if (counter == 3) {
            winner = 1;
        } else if (counter2 == 3) {
            winner = 2;
        }
        counter = 0;
        counter2 = 0;
        for (int i = 0; i < 3; i++) {
            if (board[i][c].equals(" X ")) {
                counter += 1;
            } else if (board[i][c].equals(" O ")) {
                counter2 += 1;
            }
            c -= 1;
        }
        c = 2;
        if (counter == 3) {
            winner = 1;
        } else if (counter2 == 3) {
            winner = 2;
        }
        counter = 0;
        counter2 = 0;
        return winner;
    }

    public static void twoPlayers(String phrase) {
        String sPlay, sPlay1;
        int play = 0;
        int play1 = 0;
        int c = 2;
        int winner = 0;
        String playerxo = "";
        boolean end = false;
        while (winner == 0) {
            if (c % 2 == 0) {
                playerxo = "1";
            } else {
                playerxo = "2";
            }
            sPlay = JOptionPane.showInputDialog(phrase + "\nPlayer " + playerxo + " enter a row");
            sPlay1 = JOptionPane.showInputDialog(phrase + "\nPlayer " + playerxo + " enter a column");
            play = Integer.parseInt(sPlay);
            play1 = Integer.parseInt(sPlay1);
            while (!board[play - 1][play1 - 1].equals(" - ")) {
                JOptionPane.showMessageDialog(null, "Position has beem taken!");
                sPlay = JOptionPane.showInputDialog(phrase + "\nPlayer " + playerxo + " enter a row");
                sPlay1 = JOptionPane.showInputDialog(phrase + "\nPlayer " + playerxo + " enter a column");
                play = Integer.parseInt(sPlay);
                play1 = Integer.parseInt(sPlay1);
            }
            if (c % 2 == 0) {
                board[play - 1][play1 - 1] = " X ";
            } else {
                board[play - 1][play1 - 1] = " O ";
            }

            phrase = "";
            for (int i = 0; i < 3; i++) {
                for (int x = 0; x < 3; x++) {
                    phrase += board[i][x];
                }
                phrase += "\n";
            }
            c += 1;
            winner = determineWin();
        }
        if (winner == 1) {
            JOptionPane.showMessageDialog(null, "Player 1 winner!");
        } else if (winner == 2) {
            JOptionPane.showMessageDialog(null, "Player 2 winner!");
        }
    }

    public static int displayMenu() {
        String sChoice;
        int choice;
        choice = 0;
        //condition for repetition
        while (choice < 1 || choice > 3) {
            sChoice = JOptionPane.showInputDialog("MENU \n"
                    + "1.  Player vs. Player \n"
                    + "2.  Player vs. CPU \n"
                    + "3.  Terminate program");
            choice = Integer.parseInt(sChoice);
        }// end while
        return choice;
    }//end while
}
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Mon Jun 11, 2012 11:58 pm   Post subject: Re: Help With TICTACTOE

programmer1337 @ Mon Jun 11, 2012 9:53 pm wrote:
now after 3 or 4 moves the input box just quits and the program stops without checking for a win...

That's unfortunate... perhaps you should fix that.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Zren




PostPosted: Tue Jun 12, 2012 3:06 am   Post subject: RE:Help With TICTACTOE

I can see how it get's into an infinite loop, but nothing that would make it quit.
programmer1337




PostPosted: Tue Jun 12, 2012 7:31 am   Post subject: RE:Help With TICTACTOE

can you give me a hint please
programmer1337




PostPosted: Tue Jun 12, 2012 8:24 am   Post subject: RE:Help With TICTACTOE

I found out that if you choose to pick a spot where the computer picks it turns into an infinite loop, but I dont know why?
Zren




PostPosted: Tue Jun 12, 2012 2:28 pm   Post subject: RE:Help With TICTACTOE

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Math.html#random()
Quote:
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.


Casting from double to int is the same as flooring the number.

I strongly suggest writing a randomIntInRange(min, max) or something where both the parameters are included in the range.

Tip: Make sure you test that function before including it in this project. In other words, make a new project called 'Test' that you just have a main class + main function that you use to write stand alone functions in.
programmer1337




PostPosted: Tue Jun 12, 2012 3:08 pm   Post subject: Re: RE:Help With TICTACTOE

Zren @ Tue Jun 12, 2012 2:28 pm wrote:
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Math.html#random()
Quote:
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.


Casting from double to int is the same as flooring the number.

I strongly suggest writing a randomIntInRange(min, max) or something where both the parameters are included in the range.

Tip: Make sure you test that function before including it in this project. In other words, make a new project called 'Test' that you just have a main class + main function that you use to write stand alone functions in.
Ok so I did what you said, and it has not fixed anything, my problem was that when I pick a spot in the array that the random number generator has chosen also, the input box closes and it creates an infinite loop
programmer1337




PostPosted: Wed Jun 13, 2012 11:56 am   Post subject: RE:Help With TICTACTOE

fixed Razz
Sponsor
Sponsor
Sponsor
sponsor
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  [ 8 Posts ]
Jump to:   


Style:  
Search: