I'm supposed to generate a random number of piles, with a random number of coins in each pile. I'm having trouble figuring out how to generate the random number of coins in each pile, and i don't understand why coins[] should be an array. If my logic isn't completely off...shouldn't piles be an array? I usually do alright with my projects...but this one has me stumped...
There are two classes being used. Here are the req's: Game class The Game class stores the game state and requires at least two private variables: private int numPiles; // the number of piles of coins private int [] coins; // the coins in each pile
A no arg constructor should init thegame with a random number of piles and coins. A two arg constructor should init the game with a specified number of piles and a predefined array of coins in each pile. PlayGame The PlayGame class has its own private data:
-
Player names
-
An int to keep track of Whose turn it is
-
The Game object
-
A Scanner object to accept input
The only constructor is the default constructor, which should
• ask each player their name
• set turn to 1
• Init the Game by either
o Ask how many piles in the game ( num piles)
o Ask how many stick per pile and build an array int piles[] game = new Game(numPiles, piles) or
Use the default constructor to setup a random game game = new Game()
You should also have a method public void play() that plays the game until no coins remain. Remember to switch turn back and forth between player 1 and 2. Here's all i have so far...
import java.util.Scanner;
import java.util.Random;
public class PlayGame
{
private Scanner scan = new Scanner(System.in);
private int takeCoin;
boolean whoseTurn;
//Gets players names
private void getPlayers()
{
System.out.println("Welcome to the Rob Mr. Monopoly Game.");
System.out.println("First player, what is your name?");
String firstPlayer = scan.nextLine();
System.out.println();
System.out.println("Second player, what is your name?");
String secondPlayer = scan.nextLine();
System.out.println();
}
//Gets number of coins to take
private void takingCoins()
{
takeCoin = scan.nextInt();
//Doesn't allow invalid input
if(takeCoin < 1 || takeCoin > 10)
{
System.out.println(takeCoin + "? You're already stealing,"
+ " please learn how to count.");
}
}
private void getTurn()
{
if(whoseTurn == true)
{
System.out.println(firstPlayer + " it is your turn.");
}
else
{
System.out.println(secondPlayer + " it is your turn.");
}
}
public static void main(String[] args)
{
//Create a PlayGame object
PlayGame game = new PlayGame();
//Plays game
game.play();
}
}
public class Game
{
//Variables
private int numPiles; //number of coin piles
private int [] coins; //coins in each pile
private int max = 10;
private int min = 1;
public void Game()
{
//Set random number of piles
public int getPile(int min, int max)
{
Random rand = new Random();
numPiles = rand.nextInt((max - min) + 1) + min;
return numPiles;
}
}
public void Game(int numPiles, int numCoins)
{
}
}
Aucun commentaire:
Enregistrer un commentaire