Advertisement
mmayoub

Game' 10.07.2021

Jul 10th, 2021
1,155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1. package prj10072021;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Game {
  6.     Player player1, player2;
  7.     Die d;
  8.     int boardSize;
  9.     Player currentPlayer;
  10.     Scanner in = new Scanner(System.in);
  11.  
  12.     public Game(String name1, String name2, int boardSize) {
  13.         this.player1 = new Player(name1);
  14.         this.player2 = new Player(name2);
  15.         this.boardSize = boardSize;
  16.         d = new Die();
  17.  
  18.         if (d.getNum() % 2 == 0) {
  19.             currentPlayer = player1;
  20.         } else {
  21.             currentPlayer = player2;
  22.         }
  23.     }
  24.  
  25.     public String startGame() {
  26.         while (player1.getPos() < this.boardSize && player2.getPos() < this.boardSize) {
  27.             System.out.println(currentPlayer.getName() + " take a move");
  28.             in.next();
  29.             d.roll();
  30.             int steps = d.getNum();
  31.             currentPlayer.move(steps);
  32.  
  33.             int newPos = hasLeader(currentPlayer.getPos());
  34.             if (newPos != currentPlayer.getPos()) {
  35.                 currentPlayer.move(newPos - currentPlayer.getPos());
  36.             }
  37.             newPos = hasSnake(currentPlayer.getPos());
  38.             if (newPos != currentPlayer.getPos()) {
  39.                 currentPlayer.move(newPos - currentPlayer.getPos());
  40.             }
  41.  
  42.             if (currentPlayer == player1) {
  43.                 currentPlayer = player2;
  44.             } else {
  45.                 currentPlayer = player1;
  46.             }
  47.         }
  48.  
  49.         if (player1.getPos() >= this.boardSize) {
  50.             return player1.getName();
  51.         } else {
  52.             return player2.getName();
  53.         }
  54.     }
  55.  
  56.     public int hasLeader(int n) {
  57.         if (n == 5)
  58.             return 13;
  59.         if (n == 12)
  60.             return 25;
  61.         return n;
  62.     }
  63.  
  64.     public int hasSnake(int n) {
  65.         if (n == 17)
  66.             return 10;
  67.         if (n == 29)
  68.             return 14;
  69.         return n;
  70.     }
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement