Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Shawn
- *
- */
- public class Game {
- /**
- * Instance Variables
- */
- static private Player[] players;
- /**
- * @param args Unused
- */
- public static void main(String[] args) {
- // users();
- // play();
- Game g = new Game();
- g.play();
- }
- public Game() {
- players = users();
- }
- /**
- * Users, Asks how many players there will be, and creates an array of object
- */
- public static Player[] users() {
- System.out.println("How many Players?");
- int players = TextIO.getlnInt();
- Player[] pigGame = new Player[players];
- for (int i = 0; i < pigGame.length; i++) {
- System.out.println("Player " + (i + 1) + "'s name is: ");
- String name = TextIO.getln();
- pigGame[i] = new Player(name, 0);
- }
- return pigGame;
- }
- public static boolean playerTurn(Player p) {
- boolean hold = false;
- PairOfDice rolls = new PairOfDice();
- int total = p.getScore();
- int turnTotal = total;
- System.out.println("Player: " + p.getName());
- while(!hold) {
- rolls.roll();
- int one = rolls.getDie1();
- int two = rolls.getDie2();
- if (one == 1 && two == 1) {
- System.out.println(one +", " + two);
- System.out.println("You rolled double 1s, score set to 0 and turn is ended");
- p.setScore(0);
- hold = true;
- }
- else if (one == 1 || two == 1) {
- System.out.println(one +", " + two);
- System.out.println("You rolled a 1, turn is ended");
- hold = true;
- }
- else if (one > 1 && two > 1 && one == two) {
- turnTotal = turnTotal + one + two;
- System.out.println(one +", " + two);
- System.out.println("Your Score for this turn is: " + turnTotal);
- System.out.println("You rolled doubles, you must roll again");
- }
- else if (one > 1 && two > 1 && one != two) {
- turnTotal = turnTotal + one + two;
- System.out.println(one +", " + two);
- System.out.println("Your Score for this turn is: " + turnTotal);
- System.out.println("Would you like to hold? (y/n)");
- char input = TextIO.getlnChar();
- if(input == 'y')
- hold = true;
- p.setScore(turnTotal);
- System.out.println("Your score is: " + p.getScore());
- }
- }
- if (p.getScore() >= 100) {
- return true;
- }
- else {
- return false;
- }
- }
- /**
- * Runs the pig game until a winner is found
- * @param pigGame The array of Player objects
- */
- public static void play() {
- boolean gameOver = false;
- int currentPlayer = 0;
- while(!gameOver) {
- gameOver = playerTurn(players[currentPlayer]);
- currentPlayer++;
- if(currentPlayer == players.length)
- currentPlayer = 0;
- // Maybe more here, not sure
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement