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 3 of 5 FirstFirst 12345 LastLast
Results 31 to 45 of 62
  1. #31
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,458
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    By the by, you are doing a *LOT* better than many people we've seen who are 12 weeks into a course like this. I strongly suspect you could hack this thing to make it work right now. Just that you might not be following the outline the instructor gave you.

    I personally feel like this project is being given to you a little too early. It really is ripe for the use of a base class ("Player", as the base of both ComputerPlayer and HumanPlayer), and without the ability to use that feature the gameplay is harder to code and a bit uglier. My opinion.
    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.

  2. #32
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    The biggest thing for this instructor is to get the program to work in what he has taught us. He is okay with variance here and there. Okay..here is what I have so far and I am still stuck with getting the move placed in the board after being checked for valid. I put valid on the board and called the method to the test. the same will be for if they win..but i need to get the x's and o's in the board. Here is the code for the test, human player(s), and the board, of course I have an enum but that is a given...

    Code:
    import java.util.Scanner;
    
    
    public class TicTacToeTest {
    
    	
    	public static void main(String[] args) {
    		Scanner input= new Scanner(System.in);
    		HumanPlayer hp = new HumanPlayer();
    		TicTacToeBoard bd = new TicTacToeBoard();
    		
    		System.out.println("Main Menu\n WELCOME TO TIC-TAC-TOE!");
    		System.out.println();
    		System.out.println("PLAYER 1 IS ALWAYS X ");
    		System.out.println();
    		System.out.println("1. One player game.\n2. Two player game.");
    				
    		System.out.print("Please Select: ");
    		int game= input.nextInt();
    		//System.out.printf("selection=%d", game);
    		
    		if ( game==1){
    			
    			System.out.println("One Player Game\nWho will go first?\n1. Me.\n2. The Computer.");
    		System.out.print("Please Select: ");
    		int choice= input.nextInt();		
    		System.out.printf("selection=%d", choice);}
    		     
    		
    		if (game ==2){
    			System.out.println("Two Player Game");
    			//System.out.print("PLease enter first player name:");
    			//String player1;
    			hp.getFirstPlayer();
    			hp.getSecondPlayer();
    			bd.showBoard();
    			System.out.print(hp.getPlayer1(TicTacToeEnum.X) + ", Please enter your moves\n");
    			hp.getRowMove();
    			hp.getColMove();
    			int row = hp.getRow();
    		     int col  = hp.getCol();
    			bd.isMoveValid(row, col);
    			 
    		     boolean isValid = board.isMoveValid(row, col);
    	     if (isValid) {
    		        board.move(row,col, x_or_o);
    
    		     
    		}
    	}  
    
    		}
    the board:
    Code:
    public class TicTacToeBoard 
    {
    	
    	
    	
        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();
        
       int row = hp.getRowMove();
       int col  = hp.getColMove();
      
    	  
       
       
       //private boolean move= getMove(Player1,Player2);
       private boolean isValid= isMoveValid(row,col);
       
         public  boolean isMoveValid(int row, int col){
        	  if (  row <0 || row >2 || col <0||col>2){
        		  System.out.println("invalid move");    	
         }else {
        	  board [row][col]= hp.getTicTacToeEnum() ; }
        	  
    		return isValid;
          }
         
    
         
         
    //     public boolean getMove(String Player1, String Player2){
    //    	 if  (isValid == Player1){
    //    	 {
    //    		 board[row][col]= TicTacToeEnum.X;
    //    	 }
    //    	 
    
    
    
    
    	public void setValid(boolean isValid) {
    		this.isValid = isValid;
    	}
    
    
    
    
    	public boolean isValid() {
    		return isValid;
    	}
        	  
    //   	  public void getPlayer2Move(){
    		public void check()
    	{
    	  int i;
    
    	  for(i=0; i<3; i++)  /* check rows */
    	    if((board[i][0]==board[i][1]) &&
    	     ( board[i][0]==board[i][2])!= isValid()||	    
    	      (board[0][i]==board[1][i]) &&
    	     ( board[0][i]==board[2][i])!= isValid()||	  
    	      (board[0][0]==board[1][1]) &&
    		  (board[1][1]==board[2][2])!= isValid()||
    		 ( board[0][2]==board[1][1]) &&
    		  (board[1][1]==board[2][0])!= isValid()){
    	       System.out.println("game won");}
    	  else{
    		  System.out.println("game lost");}
    	  }
    
        	 
          }
    the humanplayer:
    Code:
    import java.util.Scanner;
    
    
    public class HumanPlayer {
    	
    
    	
    	private String firstPlayer ;
    	private String secondPlayer;
    	private int rowMove;
    	private int colMove;
    //	TicTacToeBoard bd = new TicTacToeBoard();
    	
    	
    //		public HumanPlayer(String player1, String player2, Integer row, Integer column){
    //	setFirstPlayer(player1);
    //	setSecondPlayer(player2);
    //	setRowMove(row);
    //	setColMove(column);}
    
    
    
    		Scanner input = new Scanner(System.in);
    		private TicTacToeEnum x_or_o;
    	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 setRowMove(int rowMove) {
    		this.rowMove = rowMove;
    		}
    		
    	public int getRowMove(){
    		
    			System.out.print( "Enter row:");
    			rowMove= input.nextInt();
    			return rowMove;
    		
    	}
    	public void setColMove(int colMove) {
    		this.colMove = colMove;
    		}
    		public int getColMove(){
    	System.out.print( "Enter column:");
    		colMove= input.nextInt();
    		return colMove;}
    	
    	
    	public String getPlayer1(TicTacToeEnum X){
       	 return firstPlayer;
        }
    	public String getPlayer2(TicTacToeEnum O){
    		return secondPlayer;}
    	
    	public int getRow() {
    		return rowMove;
    	}
    	public int getCol(){
    		return colMove;
    	}
    	public void setTicTacToeEnum(TicTacToeEnum x_or_o) {
    		  this. x_or_o =  x_or_o;
    		}
    		 
    		public TicTacToeEnum getTicTacToeEnum() {
    		   return x_or_o;
    		}
    
    	}
    now, I have no read lines but i am getting a null expression in my boolean..

    by the way, the majority has been help from you and I have been emailing my instructor getting input also..so some of the changes are not "ignoring" you, just an adjustment from what he may want to see...this is killing me only two days left to get it and I haven't even done the computer class with random or what ever to make the computer pick..please tell me that one will be easier
    Last edited by mdnealy; 12-14-2009 at 04:08 PM.

  3. #33
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,458
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    Look, I know it may not 100% fulfill the requirements, but you need to drop the idea of 1 or 2 player game.

    I say again: Unless you can have HumanPlayer and ComputerPlayer both be subclasses of Player, it is way too difficult to do the 1 or 2 player game.

    And in any case, your way of doing two human players simply isn't going to work right.

    SIMPLIFY!

    One human player, one computer player.

    Trust me on this.

    You can tell the instructor that you understand that if both were subclasses you could make the game more general.

    Go ahead and start working on the computer player. Yes, you could start with simply picking a spot on the board at random, checking to see if it is empty. If not, you pick again. Keep picking until you find an empty cell. It's not great strategy, but it's more important to get it working.

    NOTE: One thing this means is that when you call the ComputerPlayer's "getMove( )" method, you will need to PASS IN the board as an argument! Otherwise, that method won't be able to "see" it. (The HumanPlayer actually needs the same thing, but the difference is that the board is "passed in" to the human player by displaying it on the screen. Kind of end-around of the computer process.)
    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.

  4. #34
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Okay, I know I need to get the information from the computer and place it in the board. so, on my return statement is where I have the problem. how do I get the move I want into the board. I think that is the biggest thing I cannot see. is If I need to get a value from computer class like two integers, how do I write the syntax to get the integers from computer into the board? I see the row to text but where do I put the computers stuff???

    Please see the bottom for my questions...

    Code:
    public class ComputerPlayer {
    	
    	private String computer;
    	private int row;
    	private int col;
    	private TicTacToeEnum x_or_o;
    	
    	
    	
    	
    	
    	TicTacToeBoard bd = new TicTacToeBoard();
    	HumanPlayer hp = new HumanPlayer();
    	public void setComputer(String computer) {
    		this.computer = computer;
    	}
    	public String getcomputer(TicTacToeEnum O) {
    		
    		return computer;
    	}
    	
    	
    	public void setTicTacToeEnum(TicTacToeEnum x_or_o) {
    		  this. x_or_o =  x_or_o;
    		}
    		 
    		public TicTacToeEnum getTicTacToeEnum() {
    		   return x_or_o;}
    
    	
    
    		public int getMove(int row,int col){
    		
    			if((bd.board[0][0] == TicTacToeEnum.X) && (bd.board[0][1] == TicTacToeEnum.X) && (bd.board[0][2] == TicTacToeEnum.Empty));
    			return bd.board[0][2]== getMove(0,2) ;
    			
    			if	((bd.board[1][0] == TicTacToeEnum.X) && (bd.board[1][1] == TicTacToeEnum.X) && (bd.board[1][2] == TicTacToeEnum.Empty));			
    			return bd.board[1][2];
    			
    			if{((bd.board[2][0] == TicTacToeEnum.X) && (bd.board[2][1] == TicTacToeEnum.X) && (bd.board[2][2] == TicTacToeEnum.Empty));
    			return bd.board[2][2];
    
    			else if((bd.board[0][1] == TicTacToeEnum.X) && (bd.board[0][2] == TicTacToeEnum.X) && (bd.board[0][0] == TicTacToeEnum.Empty));
    			return bd.board[0][0];
    			else if((bd.board[1][1] == TicTacToeEnum.X) && (bd.board[1][5] == TicTacToeEnum.X) && (bd.board[1][0] == TicTacToeEnum.Empty));
    			return bd.board[1][0];
    			else if((bd.board[2][1] == TicTacToeEnum.X) && (bd.board[2][8] == TicTacToeEnum.X) && (bd.board[2][0] == TicTacToeEnum.Empty));
    			return bd.board [2][0];
    
    			else if((bd.board[0][0] == TicTacToeEnum.X) && (bd.board[0][2] == TicTacToeEnum.X) && (bd.board[0][1] == TicTacToeEnum.Empty));
    			return bd.board[0][1];
    			else if((bd.board[1][0] == TicTacToeEnum.X) && (bd.board[1][5] == TicTacToeEnum.X) && (bd.board[1][4] == TicTacToeEnum.Empty));
    			return bd.board[1][1];
    			else if((bd.board[2][0] == TicTacToeEnum.X) && (bd.board[2][8] == TicTacToeEnum.X) && (bd.board[2][7] == TicTacToeEnum.Empty));
    			return bd.board[2][1];
    
    			else if((bd.board[0][0] == TicTacToeEnum.X) && (bd.board[1][0] == TicTacToeEnum.X) && (bd.board[2][0] == TicTacToeEnum.Empty));
    			return bd.board[2][0];
    			else if((bd.board[0][1] == TicTacToeEnum.X) && (bd.board[1][4] == TicTacToeEnum.X) && (bd.board[2][1] == TicTacToeEnum.Empty));
    			return bd.board[2][1];
    			else if((bd.board[0][2] == TicTacToeEnum.X) && (bd.board[1][5] == TicTacToeEnum.X) && (bd.board[2][2] == TicTacToeEnum.Empty));
    			return bd.board[2][2];
    
    			else if((bd.board[1][0] == TicTacToeEnum.X) && (bd.board[2][0] == TicTacToeEnum.X) && (bd.board[0][0] == TicTacToeEnum.Empty));
    			return bd.board[0][0];
    			else if((bd.board[1][1] == TicTacToeEnum.X) && (bd.board[2][1] == TicTacToeEnum.X) && (bd.board[0][1] == TicTacToeEnum.Empty));
    			return bd.board[0][1];
    			else if((bd.board[1][2] == TicTacToeEnum.X) && (bd.board[2][2] == TicTacToeEnum.X) && (bd.board[0][2] == TicTacToeEnum.Empty));
    			return bd.board[0][2];
    
    			else if((bd.board[0][0] == TicTacToeEnum.X) && (bd.board[2][0] == TicTacToeEnum.X) && (bd.board[1][0] == TicTacToeEnum.Empty));
    			return bd.board[1][0];
    			else if((bd.board[0][1] == TicTacToeEnum.X) && (bd.board[2][1] == TicTacToeEnum.X) && (bd.board[1][1] == TicTacToeEnum.Empty));
    			return bd.board[1][1];
    			else if((bd.board[0][2] == TicTacToeEnum.X) && (bd.board[2][2] == TicTacToeEnum.X) && (bd.board[1][2] == TicTacToeEnum.Empty));
    			return bd.board[1][2];
    
    			else if((bd.board[0][0] == TicTacToeEnum.X) && (bd.board[1][1] == TicTacToeEnum.X) && (bd.board[2][2] == TicTacToeEnum.Empty));
    			return bd.board[2][2];
    			else if((bd.board[1][1] == TicTacToeEnum.X) && (bd.board[2][2] == TicTacToeEnum.X) && (bd.board[0][0] == TicTacToeEnum.Empty));
    			return bd.board[0][0];
    			else if((bd.board[0][0] == TicTacToeEnum.X) && (bd.board[2][8] == TicTacToeEnum.X) && (bd.board[1][4] == TicTacToeEnum.Empty));
    			return bd.board[1][1];
    
    			else if((bd.board[0][2] == TicTacToeEnum.X) && (bd.board[1][1] == TicTacToeEnum.X) && (bd.board[2][0] == TicTacToeEnum.Empty));
    			return bd.board[0][2];
    			else if((bd.board[1][1] == TicTacToeEnum.X) && (bd.board[2][0] == TicTacToeEnum.X) && (bd.board[0][2] == TicTacToeEnum.Empty));
    			return bd.board[0][2];
    			else if((bd.board[0][2] == TicTacToeEnum.X) && (bd.board[2][0] == TicTacToeEnum.X) && (bd.board[1][1] == TicTacToeEnum.Empty));
    			return bd.board[1][1];
    
    			else if((bd.board[0][0] == TicTacToeEnum.X) && (bd.board[1][1] == TicTacToeEnum.O) && (bd.board[2][2] == TicTacToeEnum.X));
    			return bd.board[1][2];
    
    			else if((bd.board[0][2] == TicTacToeEnum.X) && (bd.board[1][1] == TicTacToeEnum.O) && (bd.board[2][0] == TicTacToeEnum.X));
    			return bd.board[1][0];
    
    			else if(bd.board[1][1] == TicTacToeEnum.Empty);
    			return bd.board[1][1];
    
    			else if(bd.board[0][0] == TicTacToeEnum.Empty);
    			return bd.board[0][0];
    			return location;
    			}
    the board class:
    Code:
    public class TicTacToeBoard 
    {
    	
    	//private boolean isValid()=false;
    	
        public 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 boolean isMoveValid(int row, int col) {
    		// TODO Auto-generated method stub
    		return false;
    	}
    
    
    
    
    	public void move(int row, int col) {
    		// TODO Auto-generated method stub
    		
    	}
    
    
    
    
    	
        
       
      
    	  
    //    int row = hp.getRow();
    //    int col  = hp.getCol();
    //   
    //   //private boolean move= getMove(Player1,Player2);
    //   private boolean isValid= isMoveValid(row,col);
    ////   
    //     public  boolean isMoveValid(int row, int col){
    //    	  if (  row <0 || row >2 || col <0||col>2){
    //    		  System.out.println("invalid move");    	
    //     }else {
    //    	  board [row][col]= hp.getTicTacToeEnum() ; }
    ////    	  
    //		return isValid;}}
    //      }
         
    
         
         
    //     public boolean getMove(String Player1, String Player2){
    //    	 if  (isValid == Player1){
    //    	 {
    //    		 board[row][col]= TicTacToeEnum.X;
    //    	 }
    //    	 
    
    
    
    
    //	public void setValid(boolean isValid) {
    //		this.isValid = isValid;
    //	}
    //
    //
    //
    //
    //	public boolean isValid() {
    //		return isValid;
    //	}
    
        	  
    //   	  public void getPlayer2Move(){
    //		public void check()
    //	{
    //	  int i;
    //
    //	  for(i=0; i<3; i++)  /* check rows */
    //	    if((board[i][0]==board[i][1]) &&
    //	     ( board[i][0]==board[i][2])!= isValid()||	    
    //	      (board[0][i]==board[1][i]) &&
    //	     ( board[0][i]==board[2][i])!= isValid()||	  
    //	      (board[0][0]==board[1][1]) &&
    //		  (board[1][1]==board[2][2])!= isValid()||
    //		 ( board[0][2]==board[1][1]) &&
    //		  (board[1][1]==board[2][0])!= isValid()){
    //	       System.out.println("game won");}
    //	  else{
    //		  System.out.println("game lost");}
    //	  }
    //
    //    	 
    //      }

    1. for computer class...how do I write getMove so that I can take the row and column and transfer it to the board class

    2. for the board class...I have the two integers from the computer class, where do I put them in the board class code so that it will display in the board..I know rowto text I am just not sure how to get the information there..

    3. for some reason I cannot figure out this boolean stuff...I understand it returns a true or false but I don't see how it works in the code and where to put the stuff or write the correct syntax


    deadline approaching..tomorrow at 7 pm my time (EST) hopefully i can get this *&%^ to compile...

    thanks again for all your help!!
    Last edited by mdnealy; 12-15-2009 at 02:51 PM.

  5. #35
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,458
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    You've *still* got all that stuff in the WRONG CLASS.

    Go look again at what the instructor said to put in the TicTacToeBoard class:
    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
    Oh, w.t.h. Here's how I see that class:
    Code:
    public class TicTacToeBoard 
    {
        // Requirements:
        // 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 	
    
        // Fulfills: Contains the array
        private TicTacToeEnum[][] board;
    	    
        // Fulfills: Stores X,O,Empties
        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;
        }
            
        
        // helper methods for showing the board
        private String enumToString(TicTacToeEnum playType)
        { 
            switch ( playType )
            {
                case TicTacToeEnum.X: return "X";
                case TicTacToeEnum.O: return "O";
                case else: return " ";
            }
        }
        private String rowToText(int row)
        {  
            return enumToString(board[row][0]) + " | "
                 + enumToString(board[row][1]) + " | " 
                 + enumToString(board[row][2]);
        }
        // Fufills: Print the board
        public void showBoard( )
        {
                System.out.println("| " + rowToText(0)+ " | ");
                System.out.println("----------");
                System.out.println("| " +rowToText(1)+ " | ");
                System.out.println("----------");
                System.out.println("| " +rowToText(2)+ " | ");
        }
    
        // Fullfills: Check if the move is valid?
        public boolean isMoveValid(int row, int col) 
        {
            if ( row < 0 || row > 2 || col < 0 || col > 2 ) return false;
            if ( board[row][col] != TicTacToeEnum.Empty ) return false;
            return trud;
        }
    
        // Fullfills: Set an x or o.
        public void move(int row, int col, TicTacToeEnum playType ) 
        {
            if ( isMoveValid(row,col) ) board[row][col] = playType;
        }
    
        // Fullfills: Is the game won?
        public boolean isGameTie( )
        {
             // if game won by either player, it is not a tie
             if ( isGameWonBy( TicTacToeEnum.X ) ) return false;
             if ( isGameWonBy( TicTacToeEnum.O ) ) return false;
             // if all cells are filled, it is a tie:
             for ( int row = 0; row < 3; ++row )
             {
                 for ( int col = 0; col < 3; ++col )
                 {
                     if ( board[row][col] != TicTacToeEnum.Empty ) return false;
                 }
             }
             return true;
        }
        // Fullfills: Is the game won?
        public boolean isGameWonBy( TicTacToeEnum playType )
        {
             ... and this is the tricky one that I will let you code ...
        }
    
    }
    AND THAT IS THAT! I see NO REASON for ANY other code in that class.

    Period.

    CLEARLY that class should *NOT* be aware of what kinds of playes (Human vs. Computer) there are. Clearly it should not be asking either the computer or human for a move.
    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.

  6. Users who have thanked Old Pedant for this post:

    mdnealy (12-16-2009)

  7. #36
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,458
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    And I really don't understand why you think the the ComputerPlayer class would *contain* a TicTacToeBoard or a HumanPlayer.

    W.T.H., again. A sample ComputerPlayer:

    Code:
    public class ComputerPlayer
    {
        private TicTacToeEnum x_or_o = TicTacToeEnum.X; // defaults to play X
        
        public String name() { return "Computer"; }
        public TicToeEnum getPlayType( ) { return x_or_o; }
        public void setPlayType( TicTacToeEnum e ) { x_or_o = e; }
    
        public void makeMove( TicTacToeBoard theBoard )
        {
              while ( true )
              {
                    int row = Math.floor( Math.random() * 3 );
                    int col = Math.floor( Math.random() * 3 );
                    if ( theBoard.isMoveValid( row, col ) ) 
                    {
                         theBoard.move( row, col, this.getPlayType() );
                         return;
                    }
              }
        }
    }
    Not a very intelligent move picker, but at least it should work.

    Note that if you call makeMove when the board is already full, it will loop infinitely. So make sure you don't do that.
    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.

  8. Users who have thanked Old Pedant for this post:

    mdnealy (12-16-2009)

  9. #37
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    I know you are all getting frustrated but here what I have thanks to "Old"..

    1. I have an error on the at the enumToString where the first "" says that it is never read. I changed this because it would not accept the return part of the case. So I am not sure why that warning is there but both ways gives me errors.

    2. in the test, i have gotten to the make move from the player side. I have no errors in the humanplayer class, so it must be the way that I am calling the method.

    3. Please disregard player 2 in the human player class. it is just sitting there until I can get the human vs computer running then i will work on the two player game.

    4. in computer player i had to change all code across the board to double because of the random.

    5. Here is the code for the board, test, computer and human player...

    Board:
    Code:
    public class TicTacToeBoard 
    {
        // Requirements:
        // 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 	
    
        // Fulfills: Contains the array
        private TicTacToeEnum[][] board;
    	    
        // Fulfills: Stores X,O,Empties
        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;
        }
            
        
        // helper methods for showing the board
        private String enumToString(TicTacToeEnum playType)
        { 
    
        String enumToString ="";
        if (playType ==TicTacToeEnum.X){
        enumToString = "X";
        }
        else
        if (playType==TicTacToeEnum.O){
        enumToString = "O";
        }
        return enumToString="";
        }
    
    
      
        private String rowToText(double row)
        {  
            return enumToString(board[(int)row][0]) + " | "
                 + enumToString(board[(int) row][1]) + " | " 
                 + enumToString(board[(int) row][2]);
        }
        // Fufills: Print the board
        public void showBoard( )
        {
                System.out.println("| " + rowToText(0)+ " | ");
                System.out.println("----------");
                System.out.println("| " +rowToText(1)+ " | ");
                System.out.println("----------");
                System.out.println("| " +rowToText(2)+ " | ");
        }
    
        
        public boolean isMoveValid(double row, double col) 
        {
            if ( row < 0 || row > 2 || (int)col < 0 || (int)col > 2 ) return false;
            if ( board[(int) row][ (int)col] != TicTacToeEnum.Empty ) return false;
            return true;
        }
    
       
        public void move(double row, double col, TicTacToeEnum playType ) 
        {
            if ( isMoveValid((int)row,(int) col )) board[(int) row][(int) col] = playType;
        }
    
       
        public boolean isGameTie( )
        {
             
             if ( isGameWonBy( TicTacToeEnum.X ) ) return false;
             if ( isGameWonBy( TicTacToeEnum.O ) ) return false;
            
             for ( int row = 0; row < 3; ++row )
             {
                 for ( int col = 0; col < 3; ++col )
                 {
                     if ( board[(int)row][(int)col] != TicTacToeEnum.Empty ) return false;
                 }
             }
             return true;
        }
        
        public boolean isGameWonBy( TicTacToeEnum playType )
        {
        	if ( isGameWonBy( TicTacToeEnum.X ) ) return true;  
        	else
        		if( isGameWonBy( TicTacToeEnum.X ) ) return false; 
        	
            if ( isGameWonBy( TicTacToeEnum.O ) ) return false;
            else
            	if( isGameWonBy( TicTacToeEnum.O ) ) return true; 
            
    
            
            for ( int row = 0; row < 3; ++row )
            {
                for ( int col = 0; col < 3; ++col )
                { /* check rows */
              if((board[0][0]==board[0][1] && board[0][0]==board[0][2])||(board[1][0]==board[1][1] && board[1][0]==board[1][2])||
            		  (board[2][0]==board[2][1] && board[2][0]==board[2][2])){
            	  return true;
              }
              else
            	  if((board[0][0]==board[1][0] && board[0][0]==board[2][0])||(board[0][1]==board[1][1] && board[0][1]==board[2][1])||
            			  (board[0][2]==board[1][2] && board[0][2]==board[2][2])){
            		  return true;
            	  }
            	  else
            		  if((board[0][0]==board[1][1] && board[1][1]==board[2][2])||(board[0][2]==board[1][1] && board[1][1]==board[2][0])){
            			  return true;
            		  }
              
    
                }
            }
    		return true;
        }
            
    
    }
    Test:
    Code:
    import java.util.Scanner;
    
    
    public class TicTacToeTest {
    
    	
    	public static final void main(String[] args) {
    		Scanner input= new Scanner(System.in);
    		HumanPlayer hp = new HumanPlayer();
    		TicTacToeBoard bd = new TicTacToeBoard();
    		ComputerPlayer cp = new ComputerPlayer();
    		System.out.println("Main Menu\n WELCOME TO TIC-TAC-TOE!");
    		System.out.println();
    		System.out.println("PLAYER 1 IS ALWAYS X ");
    		System.out.println();
    		System.out.println("1. One player game.\n2. Two player game.");
    				
    		System.out.print("Please Select: ");
    		int game= input.nextInt();
    		
    		
    		if ( game==1){
    			
    		System.out.println("One Player Game\nWho will go first?\n1. Me.\n2. The Computer.");
    		
    		System.out.print("Please Select: ");
    		int choice= input.nextInt();		
    		
    		
    		//do{
    		if (choice ==1){
    			
    			hp.getFirstPlayer();
    			bd.showBoard();
    			hp.makeMove(bd);
    			int row= hp.getRowMove(); 
    			int col=hp.getColMove();
                bd.move( row, col, TicTacToeEnum.X);
                bd.showBoard();
    		
    		
    		} }
    		}
    	}
    humanplayer
    Code:
    import java.util.Scanner;
    
    
    public class HumanPlayer {
    	
    	   private TicTacToeEnum x_or_o = TicTacToeEnum.O; 
    	    private int row;
    	    private int col;
    	   private String firstPlayer;
    	   private String secondPlayer;
    	    //public String secondPlayer;
    	   public TicTacToeEnum getPlayType( ) { return x_or_o; }
    	    public void setPlayType( TicTacToeEnum e ) { x_or_o = e; }
    	    Scanner input = new Scanner(System.in);
    
    	    public void makeMove( TicTacToeBoard theBoard )
    	    {
    	          while ( true )
    	          {
    	        	    
    	                System.out.print("Please enter row:");
    	                int row = input.nextInt();	        	   
    	                System.out.print("Please enter column:");	                
    	                int col = input.nextInt();
    	        	  
    	                if ( theBoard.isMoveValid(( (int)row), ((int) col ) )) 
    	                {
    	                     theBoard.move( (int)row,(int) col, this.getPlayType() );
    	                     return;}
    	                
    	          }
    
    	
    	    }
    	
    	
    	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 int getRowMove(){
    		return row;
    	}
    	
    	public int getColMove(){
    		return col;
    	}
    		    
    	}
    computer:
    Code:
    public class ComputerPlayer
    {
        private TicTacToeEnum x_or_o = TicTacToeEnum.X; 
        
        public String name() { return "Computer"; }
        public TicTacToeEnum getPlayType( ) { return x_or_o; }
        public void setPlayType( TicTacToeEnum e ) { x_or_o = e; }
    
        public void makeMove( TicTacToeBoard theBoard )
        {
              while ( true )
              {
                    double row = Math.floor( Math.random() * 3 );
                    double col = Math.floor( Math.random() * 3 );
            	  
                    if ( theBoard.isMoveValid( row,  col ) ) 
                    {
                         theBoard.move( row, col, this.getPlayType() );
                         return;
                    }
              }
        }
    }
    thanks again for your help...hopefully this will finish up tomorrow..

  10. #38
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,458
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    Clearly this code is wrong:
    Code:
        // helper methods for showing the board
        private String enumToString(TicTacToeEnum playType)
        { 
    
        String enumToString ="";
        if (playType ==TicTacToeEnum.X){
        enumToString = "X";
        }
        else
        if (playType==TicTacToeEnum.O){
        enumToString = "O";
        }
        return enumToString="";
        }
    (1) You never return anything if the playType is X or O.
    (2) You return "" (instead of a space) if the space is empty.

    I don't know why the switch didn't work for you, but let's move on. Change that code to this:
    Code:
        // helper methods for showing the board
        private String enumToString(TicTacToeEnum playType)
        { 
             if (playType ==TicTacToeEnum.X) return "X";
             if (playType==TicTacToeEnum.O) return "O";
             return " "; // a space!  not a blank string!
        }
    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.

  11. Users who have thanked Old Pedant for this post:

    mdnealy (12-16-2009)

  12. #39
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,458
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    And I refuse to go along with your attempts to have 2 human players. I say again that your approach is ALL WRONG.

    I will give you a POSSIBLE answer:
    Code:
    public class HumanPlayer 
    {
        private TicTacToeEnum x_or_o = TicTacToeEnum.O; 
        private String playerName = "Human";
        private Scanner input = new Scanner(System.in);
    
        // constructor:
        public HumanPlayer( String name, TicTacToeEnum playType )
        {
              playerName = name;
              x_or_o = playType;
        }
    
        // get/set methods (probably don't need the set methods):
        public String name() { return playerName; }
        public void setName( String name ) { playerName = name; }
        public TicTacToeEnum getPlayType( ) { return x_or_o; }
        public void setPlayType( TicTacToeEnum e ) { x_or_o = e; }
    
        public void makeMove( TicTacToeBoard theBoard )
        {
            while ( true )
            {
                System.out.print("Please enter row:");
                int row = input.nextInt();	        	   
                System.out.print("Please enter column:");	                
                int col = input.nextInt();
    
                if ( theBoard.isMoveValid(( (int)row), ((int) col ) )) 
                {
                     theBoard.move( (int)row,(int) col, this.getPlayType() );
                     return;
                }
            } // end of infinite loop
        }
    
    } // end of class
    If you really decide you want 2 human players--and it's going to be tough to code the game play for that--then you create TWO OBJECTS of class HumanPlayer. You do *NOT* try to put two people into ONE Player.
    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. Users who have thanked Old Pedant for this post:

    mdnealy (12-16-2009)

  14. #40
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,458
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    And I am not going to write all the main code for you, but let's at least show the biggest problems.
    Code:
    public class TicTacToeTest 
    {
        public static final void main(String[] args) 
        {
            Scanner input= new Scanner(System.in);
            TicTacToeBoard bd = new TicTacToeBoard();
            ComputerPlayer cp = new ComputerPlayer();
            // where do you get the NAME of the human player???
            HumanPlayer hp = new HumanPlayer();
    
            System.out.println("Main Menu\n WELCOME TO TIC-TAC-TOE!");
            System.out.println();
            System.out.println("PLAYER 1 IS ALWAYS X ");
            System.out.println();
            System.out.println("1. One player game.\n2. Two player game.");
            System.out.print("Please Select: ");
            int game= input.nextInt();
            // YOU HAVE NO CODE HERE THAT DOES ANYTHING WITH THAT IF ANSWER IS 2!
            // YOU REALLY OUGHT TO GET RID OF THAT	
     		
            System.out.println("One Player Game\nWho will go first?\n1. Me.\n2. The Computer.");
            System.out.print("Please Select: ");
            int choice= input.nextInt();		
            // AND BECAUSE YOU DON'T HAVE A COMMON BASE CLASS FOR THE
            // TWO PLAYER CLASSES, EVEN THIS QUESTION SHOULD BE TOSSED OUT FOR NOW
            // MAYBE LATER, AFTER YOU UNDERSTAND THINGS BETTER YOU CAN PUT IT IN		
            // play the game
            while ( true )
            {
    	bd.showBoard();
    	cp.makeMove(bd); // computer always goes first
                 if ( bd.isGameTie() || bd.isGameWonBy( cp.getPlayType ) ) break; //out of loop
    
                 bd.showBoard();
                 hp.makeMove(bd); // then the human
                 if ( bd.isGameTie() || bd.isGameWonBy( hp.getPlayType ) ) break; //out of loop
            }
            if ( bd.isGameTie )
            {
                  ... tie game ...
            } else if ( bd.isGameWonBy( hp.getPlayType ) ) {
                  ... human wins ...
            } else if ( bd.isGameWonBy( cp.getPlayType ) ) {
                  ... computer wins ...
            }
        } // end of main
    } // end of class
    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.

  15. Users who have thanked Old Pedant for this post:

    mdnealy (12-16-2009)

  16. #41
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,458
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    I guess I did write the main code for you. Oh, well.

    Your isGameWonBy is badly broken. Your first 6 lines make no sense whatsoever. You'll just end up doing infinite recursion.

    You never bother to check to see if the 3-in-a-row matches the argument (the player's enum that you are testing).

    You have nested for loops but never use them.

    Toss that code and start over.
    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.

  17. Users who have thanked Old Pedant for this post:

    mdnealy (12-16-2009)

  18. #42
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    nevermind say the last post and yes you actually put my program together...but sure as heck learned alot!!!
    Last edited by mdnealy; 12-16-2009 at 06:40 AM.

  19. #43
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,458
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    I'm heading for bed.

    I'll kick myself in the morning for this, but...

    Code:
        public boolean isGameWonBy( TicTacToeEnum te )
        {
            for ( int row = 0; row < 3; ++row )
            {
                 if (board[row][0] == te && board[row][1] == te && board[row][2] == te )
                      return true;
            }
            for ( int col = 0; col < 3; ++col )
            {
                 if (board[0][col] == te && board[1][col] == te && board[2][col] == te )
                      return true;
            }
            if ( board[0][0] == te && board[1][1] == te && board[2][2] == te ) 
                 return true;
    
            if ( board[0][2] == te && board[1][1] == te && board[2][0] == te ) 
                 return true;
    
            return false;
        }
    Remember the K.I.S.S. principle.


    Now, for your part, see if you can come up with a better strategy for the computer player than simply making a random move.
    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.

  20. Users who have thanked Old Pedant for this post:

    mdnealy (12-16-2009)

  21. #44
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    my board is still screwing up...i will look it over..but I am right behind you it is 2 am here...

    btw...now your forum name "Pedant" makes sense...and as far as the first part I am sure I have you beat by far...

    More tomorrow...



    btw...i am going to have to keep doing projects to learn this java stuff just because...now...

  22. #45
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,458
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    My age this year is the product of two primes.

    My age last year was not prime but had only one prime factor.

    My age the year before that was the product of three primes, two of which were the same.

    Should be able to figure it out from that.
    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 3 of 5 FirstFirst 12345 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
  •