Question: Need to write a code in Java that does the following 3 parts (I have done parts 1 and 2 bu am having trouble with
Need to write a code in Java that does the following 3 parts (I have done parts 1 and 2 bu am having trouble with part 3 if anyone can help):
Part 1 Add support for additional players
Right now there is only 1 player. I expect you to add support for an additional player (so 2 players, though if you add support for more players then I will award 5 bonus points for that) but I want this additional player to be smarter than the original (so it should be a subclass of the first with different logic). I want you to redefine makeMove so that it has logic that will enable the computer player (AI, not human input) to choose whether to take the move or not. Meaning that this new player will be able to roll the die (get a random number) and they can look at the board and before telling the board to make the move decide whether they want to attempt the move or not.
Part 2 players cannot take up the same square
Except at the starting line players may not be in the same square at the same time. If a player attempts to land on a square that someone else is on then they are moved backwards by 1. Consider where best to add this modification and justify your decision in comments where you do so.
Part 3 add an additional square type to the board called a Skip
This will skip the player forward randomly between 1 and 5 spaces. The frequency of them occurring on the board is 15% and they are depicted with an S. They also will increase the row variable.
This is the code I have so far
public class Driver
{
public static void main(String[] args)
{
BoardGame board = new BoardGame();
Player player1 = new Player(board.getStart());
int turn=0;
while (true)
{
//move
int roll = player1.makeMove();
board.movePlayer(player1, roll);
//draw everything
System.out.println(" Turn: "+(++turn));
board.draw(player1);
System.out.println();
try{
Thread.sleep(300);
}catch(Exception e){}
}
}
}
class Player
{
Square location;
String Symbol;
public Player(Square start)
{
location=start;
Symbol = "*";
}
public int makeMove()
{
int roll = (int)(Math.random()*6)+1;
return roll;
}
public void draw()
{
for (int i=0;i System.out.print(" "); System.out.print(Symbol); } } class SmartPlayer extends Player { public SmartPlayer(Square start) { super (start); Symbol = "*"; } public int makeMove(BoardGame board) { int roll = (int)(Math.random()*6)+1; if (roll==1) if (board.Board[location.X+roll].toString().equals("R")); return roll; } } class BoardGame { public final int GAMELENGTH = 50; public final float RETURN_SQUARE_FREQUENCY = 0.3f; public Square[] Board; public BoardGame() { Board = new Square[GAMELENGTH]; Board[0] = new Square(0); int row = 0; for (int i=1;i { if (Math.random() < RETURN_SQUARE_FREQUENCY && i > 6 && row < 3) { Board[i] = new Return(i); row++; } else { Board[i] = new Square(i); row = 0; } } Board[GAMELENGTH - 1] = new End(GAMELENGTH-1); } public Square getStart() { return Board[0]; } public void movePlayer(Player p, int i) { int newLocation = p.location.X + i; if (newLocation > GAMELENGTH-1) { return; } p.location.unland(p, this); p.location = Board[newLocation]; p.location.land(p, this); } public void draw(Player p) { for (int i=0;i { System.out.print("_"); } System.out.println(); for (int i=0;i { if (i == p.location.X) System.out.print(p.Symbol); else System.out.print(Board[i].draw()); } } } class Square { public int X; Player occupant; public Square(int i) { X=i; } public void land(Player p, BoardGame b) { if (occupant == null) occupant = p; else b.movePlayer (newLocation); //normal square does nothing } public void unland(Player p, BoardGame b) { occupant = null; } public String draw() { return "_"; } } class Return extends Square { public Return(int i) { super(i); } public void land(Player p, BoardGame b) { int i = -(int)(Math.random()*6)+1; b.movePlayer(p, i); } public String draw() { return "R"; } } class End extends Square { public End(int i) { super(i); } public void land(Player p, BoardGame b) { System.out.println(" GAME IS OVER"); System.out.println("Player that won: Player "+p.Symbol); System.exit(0); } public String draw() { return "E"; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
