Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 5 FirstFirst 1234 ... LastLast
Results 16 to 30 of 62
  1. #16
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Code:
     board[0][0]= TicTacToeEnum.Empty;
    	 board[0][1]= TicTacToeEnum.Empty;
    	 board[0][2]= TicTacToeEnum.Empty; 
    	 board[1][0]= TicTacToeEnum.Empty;
    	 board[1][1]= TicTacToeEnum.Empty;
    	 board[1][2]= TicTacToeEnum.Empty;
    	 board[2][0]= TicTacToeEnum.Empty;
    	 board[2][1]= TicTacToeEnum.Empty;
    	 board[2][2]= TicTacToeEnum.Empty;}}
    here is the humanPlayer class..
    Code:
    import java.util.Scanner;
    
    
    public class HumanPlayer {
    	
    	private String firstPlayer ;
    	private String secondPlayer;
    	private int row ;
    	private int column;
    	TicTacToeBoard bd = new TicTacToeBoard();	
    	
    public HumanPlayer(String Player1, String Player2){
    	setFirstPlayer(Player1);
    	setSecondPlayer(Player2);
    }
    	
    	
    	
    	
    Scanner input = new Scanner(System.in);
    	public void setFirstPlayer(String firstPlayer) {
    		this.firstPlayer = firstPlayer;
    	}
    	public String getFirstPlayer() {
    		System.out.print("please enter Player 1 name:");
    		firstPlayer= input.next();
    		return firstPlayer;
    	}
    	
    	public void setSecondPlayer(String secondPlayer) {
    		this.secondPlayer = secondPlayer;
    	}
    	public String getSecondPlayer() {
    		System.out.print("Please enter Player 2 name:");
    		secondPlayer=input.next();
    		return secondPlayer;
    	}
    	
    	public void getMove(){
    		System.out.print( firstPlayer +" Enter row:" );
    		 int row = input.nextInt();
    		 System.out.print( firstPlayer +" Enter column:");
    		 column = input.nextInt();
    			
    		//Store row and column to board
    		 
    	}
    	
    	}
    	
    	
    	public void setRow(int row) {
    		this.row = row;
    	}
    	public int getRow() {
    		return row= bd.rowToText(row);
    	}
    	public void setColumn(int column) {
    		this.column = column;
    	}
    	public int getColumn() {
    		return column;
    	}
    	
    	
    	
    		
    	}
    	
    	
    	
    	//System.out.print("Please enter first players name:\n");
    	//String player1=input.next();
    First...how do I make the program know that Player one is always the "X" and player two and the computer is always "O"?


    next. I have the input from player one. how do I transfer the input of row and column to the board ...I know i can do an if statement in the board such as if playermove == row, column then it replaces board[row][column} i just don't understand how to get it there. I have cut and pasted and typed back and forth and i am not translating what i want right.
    Last edited by mdnealy; 12-11-2009 at 09:11 PM.

  2. #17
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    No, you are really missing some very important concepts.

    The class HumanPlayer should represent *ONE* player. Period.

    Probably, you should have a base class named Player.
    And then you have both HumanPlayer and ComputerPlayer classes that derive from (extend) that class.
    And you "instantiate" as many of each kind of player as you need.

    You could have two objects of class HumanPlayer, two of class ComputerPlayer, or one of each.

    And then you have methods on Player that are *implemented* differently by the HumanPlayer and ComputerPlayer.

    For example, only:
    Code:
    public interface Player
    {
         public String getName();
         public CellNumber getMove();
    }
    public class ComputerPlayer implements Player
    {
        public String getName() { return "Computer"; }
        ...
    }
    public class HumanPlayer implements Player
    {
        private String _name;
        public void setName(String name) { _name = name; }
        public String getName() { return _name; }
        ...
    }
    And so on. Pardon me if I make a syntax error. Been writing all C++ code lately and my head can sometimes get in the wrong space.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  3. #18
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    that makes sense but we didn't get to interfaces...in the assignment tthe human player prompts and returns the row and column and stores the name of the players.

    the computer class generates and returns the value for row and column
    and the board sets the return x and o from those two classes..

  4. #19
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Fine. So have a single Player class with a flag that says whether the player is human or not.
    Code:
    public class Player
    {
        private boolean isHuman = false;
        private String _name = "Computer";
        
        public Player( String name )
        {
            _name = name;
            isHuman = ! name.equals("Computer");
        }
        
        public cellNumber getMove( )
        {
            cellNumber move = new cellNumber(-1,-1);
            if ( isHuman )
            {
                 ... prompt for and get coordinates ...
            } else {
                 ... use artificial intelligence to make the move ...
            }
            return move;
        }
        ... other methods? ...
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  5. #20
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    how does it store it on the board...cellNumber knows to go to the board and put it in?

  6. #21
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Actually, if you only have the one Player class, then the getMove() method should indeed just return void after actually putting the move into the right cell.

    So...
    Code:
    public class Player
    {
        private boolean isHuman = false;
        private String _name = null;
        private TicTacToeEnum xORo = null;
        
        public Player( String name, TicTacToeEnum whichPlayer )
        {
            _name = name;
            isHuman = ! name.equals("Computer");
            xORo = whichPlayer;
        }
        
        public void getMove( )
        {
            int rowMove;
            int colMove;
            while ( true )
            {
                if ( isHuman )
                {
                     ... prompt for and get coordinates into rowMove and colMove ...
                } else {
                     ... use artificial intelligence to make the move ...
                }
                if ( rowMove < 0 || rowMove > 2 || colMove < 0 || colMove > 2
                     board[rowMove][colMove] != TicTacToeEnum.Empty 
                ) 
                { 
                    ... invalid move message ...
                } else {
                    board[rowMove][colMove] = xOro;
                    return; // move succeeded
                }
             } // end of the infinite while loop
        }
        ... other methods? ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  7. #22
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Hope your around tomorrow...have to work..thanks for that and it makes more sense...will play with that and post tomorrow should i run into anything else. thank you so much for your help. though I have changed some stuff around to fit what he has taught us, your help has made all those pieces come together where it makes more sense. until tomorrow....

  8. #23
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Code:
    public class TicTacToeBoard 
    {
    	
    	//private boolean move=false;
    	
        private TicTacToeEnum[][] board;
    	    
    	public TicTacToeBoard(){
    			board=new TicTacToeEnum[3][3];
    			{
    	
    	 board[0][0]= TicTacToeEnum.Empty;
    	 board[0][1]= TicTacToeEnum.Empty;
    	 board[0][2]= TicTacToeEnum.Empty; 
    	 board[1][0]= TicTacToeEnum.Empty;
    	 board[1][1]= TicTacToeEnum.Empty;
    	 board[1][2]= TicTacToeEnum.Empty;
    	 board[2][0]= TicTacToeEnum.Empty;
    	 board[2][1]= TicTacToeEnum.Empty;
    	 board[2][2]= TicTacToeEnum.Empty;}}
            
        
       
          
           String rowToText(int row)
           {   //int row = hp.getRow();
               return getValue (board[row][0]) + " | "
                    + getValue(board[ row][1]) + " | " 
                    + getValue(board[row][2]);
           }
           void showBoard( )
           {
                System.out.println("| " + rowToText(0)+ " | ");
                System.out.println("----------");
                System.out.println("| " +rowToText(1)+ " | ");
                System.out.println("----------");
                System.out.println("| " +rowToText(2)+ " | ");
            }
    
                
           
           public  String getValue(TicTacToeEnum ticTacToeEnum)
           { 
        	   String letterDescription = "";
        	   if (ticTacToeEnum ==TicTacToeEnum.X){
        		   letterDescription = "X";
        	   }
        	   else
        		   if (ticTacToeEnum==TicTacToeEnum.O){
        			   letterDescription = "O";
        		   }
        	   return letterDescription;
           }
           
           HumanPlayer hp=new HumanPlayer();
    
          public void getPlayer1Move(){
        	  if (hp.getRow()<0 || hp.getRow() >2 || hp.getCol() <0 || hp.getCol() >2 & board [hp.getRow()][hp.getCol()]  != TicTacToeEnum.Empty){
        		  System.out.println("invalid move");
          }else{
        	  board [hp.getRow()][hp.getCol()] = TicTacToeEnum.X;}
          }
        	  
        	  public void getPlayer2Move(){
            	  if (hp.getRow()<0 || hp.getRow() >2 || hp.getCol() <0 || hp.getCol() >2 & board [hp.getRow()][hp.getCol()] != TicTacToeEnum.Empty){
              }else{
            	  board [hp.getRow()][hp.getCol()] = TicTacToeEnum.O;
              }
        	 
          }
    
    }
    Okay, in the above code, I am trying to say in player 1, if hp.getRow and hp.getCol are less than or greater than 2 (which are my array parameters) then it remains empty and a invalid message is generated....else...they equal x

    it prints in the board always in the 0,0 position.


    I have this working so far but the input from the user(in another class) is not printing in what they put. I usually use 1,1..and it still prints in 0,0

  9. #24
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    You still aren't thinking abstractly enough.

    Look, game play should be something along these lines:
    Code:
        public Player getOnePlayer(int which)
        {
            ... prompt with "is player " + which + " human?"
            if ( ..no.. )
            {
                  return new Player( "Computer" );
            } else {
                  ... get name of human player ...
                  return new Player( name );
            }
        }
        
        public int findWinner( )
        {
              ... scan the board to see if there is a winner ...
              ... if not, return -1 ...
              ... if a tie, return 0 ...
              ... if a winner, return player number 1 of the winner ...
        }
    
        public void playOneGame( )
        {
            Player player1 = getOnePlayer(1);
            Player player2 = getOnePlayer(2);
            int winner;
    
            while ( ( winner = findWinner() ) < 0 )
            {
                 player1.getMove( );
                 player2.getMove( );
            }
            if ( winner == 0 )
            {
                  ... message: tie game ...
            } else {
                  ... message: the winner was ...
            }
        }
    I already see some improvements to be made. For example, probably better to do
    Code:
         Player players[2];
    and then use those array elements, so that the "winner" number can refer to the winning array element.

    But never mind, those are refinements. Get the thing working first.

    Oh...and you didn't use my concept of how to make a move at all, did you?? *CLEARLY* getMove( ) should be a method on Player that does *ALL* the checking for move validity. The "mainline" code should not be making any "is this a legal move" checks, at all.

    [You could argue that there should be a separate Move class that checks for legal moves, and I'd certainly want to do that for something like Chess, but I think doing that as a method on Player is adequate for this game.]
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  10. #25
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    the problem is that we have specifics from the instructor.

    for the player class which holds the one player human and the two players only gets the names and the prompt for the row and column.
    the computer class hold the computer prompt for move

    the board class holds the moves and validates them. so i took what you wrote and broke it across.

    when i move the information of row and column from the player to the board class, for some reason it will notput the x in the right cell. it always gets put in the 0,0 cell no matter what the user inputs.

    I like your way and it makes sense to me (can't believe i am starting to understand this stuff) but I have to break it down to what he wants..here is his breakdown where things go.


    Required Classes
    TicTacToeTest.java
    • Getting the game started
    • Creating the players
    • Checking for game win/draw after a move.
    TicTacToeEnum.java
    • Enumeration
    • X, O, or Empty
    HumanPlayer.java
    • Prompting and returning Row
    • Prompting and returning Column
    • Stores the name.
    ComputerPlayer.java
    • Generates and return value for Row
    • Generates and return value for Column
    • Stores the computer’s name.
    TicTacToeBoard
    • Contains the array
    • Stores all the X,O, or empties
    • Check if the move is valid?
    • Set an x or o.
    • Is the game won?
    • Print the board

  11. #26
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Okay, so the ONLY difference between what he has and what I propose are the following:

    (1) He has a crappy design of having HumanPlayer and ComputerPlayer classes, but because they don't derive from a common base class, you can't abstract them in the TicTacToeTest class.

    Unless...

    You said that you haven't done interfaces, yet, but *have* you done base classes and sub classes???? If so, that would be the way to go.

    As it is, if there's no common base class (or interface, same thing really) then you can't easily have a game with two human players or two computer players. If that is not a requirement--if you always have one human and one computer player--then his deficiency isn't quite so bad (though it's still there and still a lmiitation).

    (2) He moves the logic for checking for a legal move to the Board class. Okay. But that means that your method on the players for getting a move has to return an array of two values: row and column. (It could also return a simple object with row and column values, which would be better but not necessary.) You do *NOT* want to ask for row and column with separate method calls, in my very strong opinion, because it complicates the situation tremendously if (for example) the human player chooses an invalid row or column. Oh, it can be coded that way. It's just ugly as pig snot.

    ********

    But that's about it. Really. You *clearly* should NEVER do stuff like you were starting to do:
    Code:
    if (hp.getRow()<0 || hp.getRow() >2 || hp.getCol() <0 || hp.getCol() >2 & board [hp.getRow()][hp.getCol()]  != TicTacToeEnum.Empty){
        		  System.out.println("invalid move");
          }else{
    Ugh. So each time you called hp.getRow() you would go out and ask the human to input a row number??? Come on!

    No!

    *********

    So...first question? Have you studied base and derived classes yet??? Because, if so, I think *clearly* you should have:
    Code:
    public class Player
    {
         String _name;
         String getName() { return _name; }
         String setName(String name) { _name = name; }
         int[] getMove( ) { return new int[2]; }
    }
    public class HumanPlayer extends Player
    {
         int[] getMove( )
         {
              int move[2];
              move[0] = ... prompt for row number ...
              move[1] = ... prompt for column number ...
              return move;
         }
    }
    public class ComputerPlayer extends Player
    {
         int[] getMove( )
         {
              int move[2];
              move[0] = ... use AI to choose row ...
              move[1] = ... ditto for column ...
              return move;
         }
    }
    Pardon me if I make some silly syntax errors. Been doing all C++ lately and my Java gets fuzzier as the weeks go by.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  12. #27
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    forgot to put private/public on members of the classes, but you get the idea.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  13. #28
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    What I'm really curious about: Why haven't you created the classes the way he describes them???

    In your code in post #23 you are showing your TicTacToeBoard class. And it holds the board. And it initialized the baord.

    But then inside *THAT* class you create a new HumanPlayer and you have methods for getPlayer1Move and getPlayer2Move. Does that match at all the model he gave you?? HINT: No.

    I quote you:
    Required Classes
    TicTacToeTest.java
    • Getting the game started
    • Creating the players
    • Checking for game win/draw after a move.
    So why not start by creating the 4 classes he requires with dummy methods/members to represent the qualities/actions he requires. Then start filling in the details. Top down. Not bottom up.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  14. #29
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    i created to the new human player to be able to pull the method from the humanplayer class. okay so, if i make the row and column an array for getting the information how to I move that over to the board to fill the spots on the board?

    we got to arrays enumeration, and time. that is it. I presumed creating the players in the test class was to choose which game a one player or two player game.
    store the names in human class of first player or second player then computer class the computer name.

    so, a very simple code that I guess could be done in a couple of classes is spread out. you show me the human and computer together but I have to separate them. then move whatever each of those classes do over to the board. Remember, I walked in to this class 12 weeks ago, going 2.5 hours a week with NO java knowledge. I have BASICS of some vb and c# but they did not get as far as this.

    so, with that, if I make one method move which contains the array for row and column which to me looks like what i did with the board how do I incorporate the row/column array with the board...

    maybe I am just a little slow and not seeing the whole picture..I thought i was doing good, got everything to work until I hit putting in the cells...
    Last edited by mdnealy; 12-14-2009 at 12:30 AM.

  15. #30
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    You really are close.

    And if you don't see how to use the array, then okay, go back to getting row and column separately. It's not ideal, but it will work in this simple system.

    I mildly disagree with the "division of labor" given by your instructor, but not so much to call it "wrong".

    Just go, again, and examine the classes he wants and the methods/members he wants and implement all of them, first. Even if some of the implementations are temporarily dummy ones.

    It's really hard for me to know exactly how close/far you are when I only see fragments of the total code. But you've got a lot of the principles right, so I think you just need some "tweaks".

    *******

    EDIT: To explain: The reason it's clumsy to get row and column separately from the computer player is that your "artificial intelligence" code for having the computer make a move *MUST* choose both row and column at the same time. (If the computer picked, say, row 1 first and then you call and ask it for a column, it might be completely hosed: there might be no cells still open in row 1.)

    So clearly when you call for the row alone, the algorithm has to actually go find a single cell, store both row and column of that cell internal to the ComputerPlayer class, and then return them one at a time.

    Okay, it works. But if you ever called for getColumn() *without* calling getRow() first, you'd get a wrong answer (either no answer at all or the same column as for the prior computer move).

    Make sense?

    It's unfortunate that he wants the two players as separate classes and yet you can't use a common base class for them (yet). I would say that it means that writing the gameplay is too difficult unless you assume that the computer player always goes first (or always second...but in any case always the same position).

    Because your game play needs to look like this:
    Code:
    TicTacToeTest.java
    • Getting the game started
    • Creating the players
    • Checking for game win/draw after a move.
    So:
    Code:
    public class TicTacToeTest
    {
        // Getting the game started
        TicTacToeBoard board = new TicTacToeBoard();
    
        // Creating the players
        ComputerPlayer p1 = new ComputerPlayer( );
        HumanPlayer p2 = new HumanPlayer( ); 
    
        while ( true )
        {
            int[] p1Move = p1.getMove( );
            board.setMoveIfValid( p1Move, TicTacToeEnum.X );
            if ( board.gameOver( ) ) break; // out of loop
            int[] p2Move = p2.getMove( );
            board.setMoveIfValid( p2Move, TicTacToeEnum.O );
            if ( board.gameOver( ) ) break; // out of loop
        }
        if ( board.gameIsTie( ) ) 
        {
            ... write out Tie message ...
        } else {
           TicTacToeEnum winner = board.getWinner( );
           ... write out winner ...
        }
    }
    But, as I said, if you are uncomfortable with the idea of getting the array for the move, you can finesse it:
    Code:
        while ( true )
        {
            int p1Row = p1.getRowMove( );
            int p1Col = p1.getColMove( );
            board.setMoveIfValid( p1Row, p1Col, TicTacToeEnum.X );
            if ( board.gameOver( ) ) break; // out of loop
            int p2Row = p2.getRowMove( );
            int p2Col = p2.getColMove( );
            board.setMoveIfValid( p2Row, p2Col, TicTacToeEnum.O );
            if ( board.gameOver( ) ) break; // out of loop
        }
    I think it's clumsier, especially for the computer player, to get the row and column with separate method calls. But clearly it can be done.
    Last edited by Old Pedant; 12-14-2009 at 06:19 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


 
Page 2 of 5 FirstFirst 1234 ... LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •