#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Set/Get method problem


    I am currently taking a introductory Java class at school, and we have been assigned to do a project, which i chose to do a tic tac toe game. THEN he said it must use GUI...says the professor who hasn't taught us anything about GUI besides what a 8 year old could figure out. He told the class to Google our project, and unbelievably he said to copy a code off of the web that someone else had already done, well that's very helpful as a lot of the codes about such a project we don't understand in the first place, let alone implementing GUI into them.
    i got a code and I've looked it over and messed around with it for awhile now and I mostly understand how it works, without using GUI. I am now trying to use GUI for setting the prompts (i.e Various messages such as the rules of the game, which of the two players won or whether it was a draw, asking the user the player's name, what marker they will play with etc.) and i am having trouble. When the dialog box appears and i input the player's name, it will not send it to the other class, i'm sure it's not that complicated but I've been looking through our book and can't find anything close to being helpful. I greatly appreciate any help. I, as well as the rest of my class, are just way over our heads on this. i know i could just Google tic tac toe using GUI, but I'm trying to actually learn this, since i'm majoring in computer science, i want to learn it, not just copy and paste it.


    Here is my Main class:

    Code:
    package com.javadb.tictactoe;
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.JOptionPane;
    
    
    
    
    
    
     public class Main {
    
     public void play() {
    
     String P1, P2;
    
     TicTacToe game = new TicTacToe();
    
    // This displays the rules of the game in a pop up text box and has no input.
     String message =
    String.format("Welcome! This game requires two players, GRAB A FRIEND!\n\nPlayers take turns marking a square with their respective letter.\n\nOnly squares empty squares can be selected.\n\nOnce a player has selected three squares in a row, the game is over and they are declared winner!\n\nIf all squares are marked with neither player having a series of 3 consecutive squares (horizontal, vertical, or diagonal), the game ends resulting in a draw.\n" );
    JOptionPane.showMessageDialog(null, message );
    
    
    //this is where I started to change it into GUI, the user inputs his name into a textbox, but I connot figure out how to set that input, saved here as String P1, to Player1 for the other class to capture.
    
     P1 = JOptionPane.showInputDialog("Enter player one's name:" );
     game.setPlayer1(game.getPrompt());
    
    
    
     P2 = JOptionPane.showInputDialog("Enter player two's name" );
     game.setPlayer2(game.getPrompt());
    
    boolean markerOk = false;
    while (!markerOk) {
    String message3 =
    JOptionPane.showInputDialog( "Select a letter to use as " + game.getPlayer1() + "'s marker: " );
    String marker = game.getPrompt();
    
    
                if (marker.length() == 1 &&
                        Character.isLetter(marker.toCharArray()[0])) {
                    markerOk = true;
                    game.setMarker1(marker.toCharArray()[0]);
                } else {
                    	String message4 =
    			JOptionPane.showInputDialog( "Invalid marker, try again." );
                	   }
            }
    
            markerOk = false;
            while (!markerOk) {
                String message5 =
    	    JOptionPane.showInputDialog( "Select a letter to use as " + game.getPlayer2() + "'s marker: " );
                String marker = game.getPrompt();
    
                if (marker.length() == 1 &&
                    Character.isLetter(marker.toCharArray()[0])) {
                    markerOk = true;
                    game.setMarker2(marker.toCharArray()[0]);
                } else {
                    String message4 =
    		JOptionPane.showInputDialog( "Invalid selection, please retry." );
                }
            }
    
    boolean continuePlaying = true;
    
            while (continuePlaying) {
    
                game.init();
                System.out.println(game.drawBoard());
                System.out.println();
    
                String player = null;
                while (!game.winner() && game.getPlays() < 9) {
                    player = game.getCurrentPlayer() == 1 ? game.getPlayer1() : game.getPlayer2();
                    boolean validPick = false;
                    while (!validPick) {
                        String message5 =
    					String.format( "It is " + player + "'s turn. Pick a square (1-9): ");
    					JOptionPane.showMessageDialog(null, message5 );
                        String square = game.getPrompt();
                        if (square.length() == 1 && Character.isDigit(square.toCharArray()[0])) {
                            int pick = 0;
                            try {
                                pick = Integer.parseInt(square);
                            } catch (NumberFormatException e) {
                                //Do nothing here, it'll evaluate as an invalid pick on the next row.
                            }
                            validPick = game.placeMarker(pick);
                        }
                        if (!validPick) {
                            String message6 =
    			String.format( "This square can not be selected. Please try again.");
    JOptionPane.showMessageDialog(null, message6 );
                        }
                    }
                    game.switchPlayers();
                    System.out.println();
                    System.out.println(game.drawBoard());
                    System.out.println();
                }
                if (game.winner()) {
                    String message6 =
    		String.format( "Game Over - " + player + " WINS!!!");						JOptionPane.showMessageDialog(null, message6 );
                } else {
                	String message7 =
    		String.format( "Game Over. Draw :( ");						JOptionPane.showMessageDialog(null, message7 );
    
    					}
    
                System.out.println();
                String message8 =						JOptionPane.showInputDialog( "Would you like to play again? Y/N");
                             String choice = game.getPrompt();
    
                if (!choice.equalsIgnoreCase("Y")) {
                    continuePlaying = false;
                }
            }
        }
        public static void main(String[] args) {
            Main main = new Main();
            main.play();
        }
    }



    This is the TicTacToe class, which is the game dynamics:

    Code:
    package com.javadb.tictactoe;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
     public class TicTacToe {
    
     private char[][] board = new char[3][3];
     private String player1;
     private String player2;
     private int currentPlayer;
     private char marker1;
     private char marker2;
     private int plays;
     private BufferedReader reader =
     new BufferedReader(new InputStreamReader(System.in));
    
     protected void init() {
     int counter = 0;
     for (int i = 0; i < 3; i++) {
     for (int i1 = 0; i1 < 3; i1++) {
     board[i][i1] = Character.forDigit(++counter, 10);
     }
     }
     currentPlayer = 1;
     plays = 0;
     }
    
     protected void switchPlayers() {
     if (getCurrentPlayer() == 1) {
     setCurrentPlayer(2);
     } else {
     setCurrentPlayer(1);
     }
     setPlays(getPlays() + 1);
     }
    
     protected boolean placeMarker(int play) {
     for (int i = 0; i < 3; i++) {
     for (int i1 = 0; i1 < 3; i1++) {
     if (board[i][i1] == Character.forDigit(play, 10)) {
     board[i][i1] = (getCurrentPlayer() == 1) ? getMarker1() : getMarker2();
     return true;
     }
     }
     }
     return false;
     }
    
    
    
     protected boolean winner() {
     //Checking rows
     char current = ' ';
     for (int i = 0; i < 3; i++) {
     int i1 = 0;
     for (i1 = 0; i1 < 3; i1++) {
     if (!Character.isLetter(board[i][i1])) {
     break;
     }
     if (i1 == 0) {
     current = board[i][i1];
     } else if (current != board[i][i1]) {
     break;
     }
     if (i1 == 2) {
     //Found winner
     return true;
     }
     }
     }
     //Checking columns
     for (int i = 0; i < 3; i++) {
     current = ' ';
     int i1 = 0;
     for (i1 = 0; i1 < 3; i1++) {
     if (!Character.isLetter(board[i1][i])) {
     break;
     }
     if (i1 == 0) {
     current = board[i1][i];
     } else if (current != board[i1][i]) {
     break;
     }
     if (i1 == 2) {
     //Found winner
     return true;
     }
     }
     }
     //Checking diagonals
     current = board[0][0];
     if (Character.isLetter(current) && board[1][1] == current && board[2][2] == current)
     {
     return true;
     }
    
     current = board[2][0];
     if (Character.isLetter(current) && board[1][1] == current && board[0][2] == current)
    {
     return true;
    }
    
     return false;
     }
    
    
    
    
    
    
    //this is where i belive my problem is coming from, as i don't completely understand the purpose of this method.
    
    
     protected String getPrompt() {
     String prompt = "";
     try {
     prompt = reader.readLine();
     } catch (IOException ex) {
     ex.printStackTrace();
     }
     return prompt;
     }
    
    
     protected String drawBoard()
     	 {
     		StringBuilder builder = new StringBuilder("Game board: \n");
     		for (int i = 0; i < 3; i++)
    
     			 {
     				for (int i1 = 0; i1 < 3; i1++)
     					 {
     						builder.append("[" + board[i][i1] + "]");
     					 }
    		 		builder.append("\n");
    
     			 }
    		 return builder.toString();
    
     	 }
    
     public int getCurrentPlayer()
     {
     return currentPlayer;
     }
    
    
     public void setCurrentPlayer(int currentPlayer)
     {
     this.currentPlayer = currentPlayer;
     }
    
    
     public char getMarker1()
     {
     return marker1;
     }
    
    
     public void setMarker1(char marker1)
     {
     this.marker1 = marker1;
     }
    
    
     public char getMarker2()
     {
     return marker2;
     }
    
    
     public void setMarker2(char marker2)
     {
     this.marker2 = marker2;
     }
    
    
     public int getPlays()
     {
     return plays;
     }
    
    
     public void setPlays(int plays)
     {
     this.plays = plays;
     }
    
    
    
    // I understand that this getPlayer1() mehtod calls from the other class game.setPlayer1() to obtain the input from the user.
     public String getPlayer1()
     {
     return player1;
     }
    
    
    //then this changes the input into the private String called player1
     public void setPlayer1(String player1)
     {
     this.player1 = player1;
     }
    
    
     public String getPlayer2()
     {
     return player2;
     }
    
    
     public void setPlayer2(String player2)
     {
     this.player2 = player2;
     }
    
    
     }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    If there is a better way for me to put up my codes than that let me know I'm new to the forum and not sure what each "Wrap" does yet.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    Ok i edited the original post to try to make it easier to read. sorry for the confusion
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    edited

IMN logo majestic logo threadwatch logo seochat tools logo