Odd or even game with Java

simple console game with random number generator

Page 1 of 1

10 Replies - 8252 Views - Last Post: 16 February 2009 - 11:44 PM Rate Topic: -----

#1 LAjavaguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 19-September 08

Odd or even game with Java

Posted 16 February 2009 - 09:13 AM

Objective: create a game using simple java programming that allows a human player to play against the computer, or have the computer play against itself. The game is Odds or Evens where the human player enters a 1 or a 2 as their Ďhandí and the computerís number is generated by a Math.random function. To make things simple, I decided that the human will always Ďcallí Evens and the computer will always be Odds. Additions to the basic structure would be: have the loser pay the winner of the hand the sum of the throw, ie, if human entered a 2 and the computer threw a 1, the human loses and would pay the computer $3. If the computer threw a 2 as well, it would pay the human $4. Another addition would be to develop a strategy for the computer to play against the human where it threw a 1 or a 2 some x% of the time, and vary x to get different strategies.

Right now, my program compiles (just) and runs, but if I throw a 2 and always lose if I throw a 1, so I know that I have a bug where itís only reading the userís input and not adding the computerís random number to get the total of the throw. I There are 3 classes, Game, Player, and Test -I know most of the issues are in the player class.

I know I have a lot of issues with it, but Iíve been looking at it and trying to get this going since yesterday morning, so some fresh eyes and comments would really help!Thanks!!

Player class:
import java.util.Scanner;

public class Player{
	
	public boolean isHuman;
	public int score =0;
	private Scanner input;
	
	public Player(int human){
		if(human > 0)
		isHuman=true;
		else
		isHuman=false;
		}
	public void play(){
		if(isHuman)
			playHuman();
		else
			playComp();
		}
	
	public void playHuman(){
		input =new Scanner(System.in);
		int current = 0;
		//int payment =0;
		int again =1;
			
		while (again==1){
			System.out.println("Enter a 1 or 2 for your throw");
			current = input.nextInt();
			int value = (int) Math.random() * 3;
			
			score = (current + value);
				if(score % 2==0)
				System.out.println("You Win!");
				else
				System.out.println("Sorry, you lose.");
		}
	}
	
	public void playComp(){
		int current =0;
		int payment =0;
		int value = (int) Math.random() * 5;
		score = (current + value);
	}
		
}


Game class:
import java.util.Scanner;

public class Game {

	private Player p1; 			//human
	private Player p2;			//computer
	Scanner scan = new Scanner(System.in);
	
	public Game(){
		p1 = new Player(1);
		p2 = new Player(0);
	}
	
	public void play()
	{
		p1.play();
		p2.play();
		}
			
}


Test class:
public class Test {

	public static void main(String[] args) {
	System.out.println("Welcome to the game of Odds or Evens");
	Game g = new Game();
	g.play();
	System.out.println("Thanks for playing");

	} // end main   
} // end class



Is This A Good Question/Topic? 0
  • +

Replies To: Odd or even game with Java

#2 BlakeJustBlake  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 26
  • View blog
  • Posts: 441
  • Joined: 15-February 09

Re: Odd or even game with Java

Posted 16 February 2009 - 09:39 AM

I don't really get the point of the game or how it works or whatever. But it seems like neither of the objects are communicating at all to be able to give each other numbers to add together.
Was This Post Helpful? 0
  • +
  • -

#3 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,620
  • Joined: 15-May 08

Re: Odd or even game with Java

Posted 16 February 2009 - 09:45 AM

You are doing the game in the Player Class instead of the game class ? The player class should only define if the player is human or computer, then what should occur is the Game class should take over the players and run the game. Not the Player class handling a gae
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Odd or even game with Java

Posted 16 February 2009 - 10:22 AM

Title edited to be more descriptive

PsychoCoder :)
Was This Post Helpful? 0
  • +
  • -

#5 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,620
  • Joined: 15-May 08

Re: Odd or even game with Java

Posted 16 February 2009 - 10:41 AM

Ok i quickly bashed this up

public class Player {
    
    private boolean isHuman;
    private String pName;
    public int score;
    
    // default constructor
    public Player() {    }
    
    // initialising constructor
    public Player(boolean type, String aName)
    {
        this.isHuman = type;
        this.pName = aName;
        this.score = 0;
    }
    
    public void pChangeType(boolean pType)  {   isHuman = pType;    }   // change player type
    public void pChangeName(String aName)   {   pName = aName;      }   // change player name
    
    public boolean getStatus()  {   return isHuman;  }  // returns player type
    public String getName()     {   return pName;    }  // returns player name
    
}



import java.util.*;

public class Game {
    
    private Player playerOne;
    private Player playerTwo;
    private boolean gameType;   //true == player v comp, false  = comp v comp
    
    public Game(boolean gType) 
    {        
        this.gameType = gType;
        
        if(gameType == true)
        {
            // begin a player v computer game
            playerOne = new Player(true, "Player1");
            playerTwo = new Player(false, "Computer");
            
            // proceed with the player v comp game
            this.DoPlayerVComputerGame();
            
        }
        else 
        {
            // begin a computer v computer game
            playerOne = new Player(false, "Computer1");
            playerTwo = new Player(false, "Computer2");
            this.DoComputerVComputerGame();
        }
        
    }
    
    private void DoComputerVComputerGame()
    {
        // the computer v computer game function
    }
        
    private void DoPlayerVComputerGame()
    {
        // the player v computer game
        int playerWishesToPlay = 1;
        int pCurrent, cCurrent, value, cValue;
        Scanner playerScan = new Scanner(System.in);
        
        while (playerWishesToPlay == 1)
        {
            System.out.println("Enter 1 or 0");
            pCurrent = playerScan.nextInt();
            
            value = (int) Math.random() * 3;
            playerOne.score = pCurrent + value;           
            
            
            cCurrent = -1;  // set computer guess
                            // need to generate a random value
                      
            cCurrent = (int)( Math.random() * 2);
            if (cCurrent > 1) cCurrent = 1;
            System.out.println("Computer Guess : " + cCurrent);
            cValue = (int)Math.random() * 3;
            
            playerTwo.score = cCurrent + cValue; // computers score
            
                      
            if (playerOne.score %2 == 0)
                System.out.println("Player Wins");
            else System.out.println("You Loose");
            
            System.out.println("Continue play ? 1 == yes 2 == no ");
            playerWishesToPlay = playerScan.nextInt();
        }
    }
}



import java.util.*;
import java.math.*;

public class GamePlay {
	
	private int iType;
	private Game myGame;
	
	private Scanner input = new Scanner(System.in);
	public GamePlay() {
	}
	
	private void beginPlay()
	{
		System.out.println("GAME OF TRUE OR FALSE\n============================\n");
		System.out.println("Player vs Computer enter 0\nComputer vs Computer enter 1\n = ");
		iType = input.nextInt();
		if(iType == 0)
		myGame = new Game(true);	// player v comp
		else myGame = new Game(false);  // comp v comp
		
	}
	
	public static void main(String[] args)
	{
		GamePlay myGame = new GamePlay();
		myGame.beginPlay();
		
	}
}



Hopefully this will get you started, as is the game works for Player vs Computer - you will need to incorporate the function for when its Computer v Computer :)
Was This Post Helpful? 1
  • +
  • -

#6 LAjavaguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 19-September 08

Re: Odd or even game with Java

Posted 16 February 2009 - 12:36 PM

Thanks for your help. I got it to run, but it's the same problem; everytime I choose a 1, I lose, and everytime I choose 0, I win. So it still seems to not be using the math.random function and adding the computer's guess to get a number between 1 and 4.

View Postbbq, on 16 Feb, 2009 - 09:41 AM, said:

Ok i quickly bashed this up

public class Player {
    
    private boolean isHuman;
    private String pName;
    public int score;
    
    // default constructor
    public Player() {    }
    
    // initialising constructor
    public Player(boolean type, String aName)
    {
        this.isHuman = type;
        this.pName = aName;
        this.score = 0;
    }
    
    public void pChangeType(boolean pType)  {   isHuman = pType;    }   // change player type
    public void pChangeName(String aName)   {   pName = aName;      }   // change player name
    
    public boolean getStatus()  {   return isHuman;  }  // returns player type
    public String getName()     {   return pName;    }  // returns player name
    
}



import java.util.*;

public class Game {
    
    private Player playerOne;
    private Player playerTwo;
    private boolean gameType;   //true == player v comp, false  = comp v comp
    
    public Game(boolean gType) 
    {        
        this.gameType = gType;
        
        if(gameType == true)
        {
            // begin a player v computer game
            playerOne = new Player(true, "Player1");
            playerTwo = new Player(false, "Computer");
            
            // proceed with the player v comp game
            this.DoPlayerVComputerGame();
            
        }
        else 
        {
            // begin a computer v computer game
            playerOne = new Player(false, "Computer1");
            playerTwo = new Player(false, "Computer2");
            this.DoComputerVComputerGame();
        }
        
    }
    
    private void DoComputerVComputerGame()
    {
        // the computer v computer game function
    }
        
    private void DoPlayerVComputerGame()
    {
        // the player v computer game
        int playerWishesToPlay = 1;
        int pCurrent, cCurrent, value, cValue;
        Scanner playerScan = new Scanner(System.in);
        
        while (playerWishesToPlay == 1)
        {
            System.out.println("Enter 1 or 0");
            pCurrent = playerScan.nextInt();
            
            value = (int) Math.random() * 3;
            playerOne.score = pCurrent + value;           
            
            
            cCurrent = -1;  // set computer guess
                            // need to generate a random value
                      
            cCurrent = (int)( Math.random() * 2);
            if (cCurrent > 1) cCurrent = 1;
            System.out.println("Computer Guess : " + cCurrent);
            cValue = (int)Math.random() * 3;
            
            playerTwo.score = cCurrent + cValue; // computers score
            
                      
            if (playerOne.score %2 == 0)
                System.out.println("Player Wins");
            else System.out.println("You Loose");
            
            System.out.println("Continue play ? 1 == yes 2 == no ");
            playerWishesToPlay = playerScan.nextInt();
        }
    }
}



import java.util.*;
import java.math.*;

public class GamePlay {
	
	private int iType;
	private Game myGame;
	
	private Scanner input = new Scanner(System.in);
	public GamePlay() {
	}
	
	private void beginPlay()
	{
		System.out.println("GAME OF TRUE OR FALSE\n============================\n");
		System.out.println("Player vs Computer enter 0\nComputer vs Computer enter 1\n = ");
		iType = input.nextInt();
		if(iType == 0)
		myGame = new Game(true);	// player v comp
		else myGame = new Game(false);  // comp v comp
		
	}
	
	public static void main(String[] args)
	{
		GamePlay myGame = new GamePlay();
		myGame.beginPlay();
		
	}
}



Hopefully this will get you started, as is the game works for Player vs Computer - you will need to incorporate the function for when its Computer v Computer :)

Was This Post Helpful? 0
  • +
  • -

#7 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,620
  • Joined: 15-May 08

Re: Odd or even game with Java

Posted 16 February 2009 - 12:50 PM

Oh i see - gotchya, well the thing is the Math.random() function produces a double from memory between 0 and one, meaning.... 0.1783 0.2455, 0.736 etc... So if you need to produce a number between one and four then you will need to do some manipulation of it... For instance if you take the (int) of any value you will always get 0 because there won't be a whole int... You may need to do

(int)(Math.random() * 10) 



meaning that if say the function produces
0.377
it will then be
(int)3.77 meaning
3

So you will need to use that sort of thing to ensure the number generated is between your limits...

So for instance
int k = -1

while (!k >0 && !k <4)
k = (int)(Math.random() *10 );



pretty sure that would help you.

Anyway let me know how you go :)
Was This Post Helpful? 0
  • +
  • -

#8 LAjavaguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 19-September 08

Re: Odd or even game with Java

Posted 16 February 2009 - 01:27 PM

Yeah, I saw that and fixed the problem by adding a 1 to the number generated by math.random, but I'm still losing all the time. It's something with how the scores are totaled. I have to play around more with it -I spend all day yesterday just trying to get the part that I posted earlier -ugh. Thanks for your help!

View Postbbq, on 16 Feb, 2009 - 11:50 AM, said:

Oh i see - gotchya, well the thing is the Math.random() function produces a double from memory between 0 and one, meaning.... 0.1783 0.2455, 0.736 etc... So if you need to produce a number between one and four then you will need to do some manipulation of it... For instance if you take the (int) of any value you will always get 0 because there won't be a whole int... You may need to do

(int)(Math.random() * 10) 



meaning that if say the function produces
0.377
it will then be
(int)3.77 meaning
3

So you will need to use that sort of thing to ensure the number generated is between your limits...

So for instance
int k = -1

while (!k >0 && !k <4)
k = (int)(Math.random() *10 );



pretty sure that would help you.

Anyway let me know how you go :)

Was This Post Helpful? 0
  • +
  • -

#9 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Odd or even game with Java

Posted 16 February 2009 - 10:04 PM

i didnt really get it, when are you supposed to win and lose?
Was This Post Helpful? 0
  • +
  • -

#10 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,620
  • Joined: 15-May 08

Re: Odd or even game with Java

Posted 16 February 2009 - 11:30 PM

View Postmostyfriedman, on 16 Feb, 2009 - 09:04 PM, said:

i didnt really get it, when are you supposed to win and lose?


Neither did i - hence why i didn't really address it, shouldn't there be a player that wins ? Not just the first player...

You are meant to win i think when you get even by the looks of it, as in x % 2 = 0 meaning even... Not 100% sure if thats all there is too it.
Was This Post Helpful? 0
  • +
  • -

#11 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Odd or even game with Java

Posted 16 February 2009 - 11:44 PM

can you comment the player class
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1