Java Project. Write an algorithm that represents a tabletop game, this game has
ID: 3905812 • Letter: J
Question
Java Project.
Write an algorithm that represents a tabletop game, this game has two players and 49 blocks, the players start at the starting position and rolls two dice that have 6 sides, then the two dice are then added after being rolled for the players to move into position, one's one players role his dice he moves into position (the game must show every step taken by players throughout the game) and lastly, as they play, there are some blocks that are bad, meaning the player takes two steps back and there are certain blocks that are good, meaning that the player takes two steps forward.
Requirements:
- use of loops
- use of methods
- user-friendly
- a maximum of three obstacles, each for good and bad.
- use a switch case as the board game.
Explanation / Answer
Assumption :
CODE:
import java.util.*;
/* Name of the class has to be "Main" only if the class is public. */
class Game
{
static int[][] p = new int[2][2]; // saves the place of player 1 and player 2
// p[0][i] saves player 1
// p[1][i] saves player 2
public static void P (int[][] map, int k) // prints the map of the board
{
System.out.print(" ");
int i,j;
System.out.println();
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
if(map[i][j]==0)
System.out.print("|| ");
else System.out.print("||" + map[i][j]);
}
System.out.println("||");
}
System.out.println();
}
public static void P (int[][] map) // prints the map of the board
{
System.out.print(" ");
int i,j;
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
if(p[0][0]==i&&p[0][1]==j)
System.out.print("||1");
else if(p[1][0]==i&&p[1][1]==j)
System.out.print("||2");
else System.out.print("|| ");
}
System.out.println("||");
}
System.out.println();
}
public static int[][] init(int[][] map) // initializes the map of the game
{
int x,y,a,b;
for(int i = 0; i<7; i++)
for(int j = 0; j<7; j++)
map[i][j] = 0;
Scanner sc=new Scanner(System.in);
System.out.print(" How many good/bad blocks you want for your game?");
System.out.println("Enter n, if you want want a max of n good and n bad blocks. n <= 3");
int n = sc.nextInt();
if(n > 3)
n = 3;
for(int i = 0; i<n; i++)
{ // no good or bad blocks on first or last row
x = (int)(5*Math.random()+1);
y = (int)(7*Math.random());
a = (int)(5*Math.random()+1);
b = (int)(7*Math.random());
map[x][y] = 3; // good block
map[a][b] = 4; // bad block
}
p[0][0] = 0; // putting the players in the starting point
p[0][1] = 0;
p[1][0] = 0;
p[1][1] = 0;
return map;
}
public static int rollDie() // gives a random generated value for the die
{
int x = (int)(6*Math.random()+1);
int y = (int)(6*Math.random()+1);
System.out.println(x + "," + y);
if(x==y)
return (-(x+y));
return x+y;
}
public static void move(int player, int[][] map, int die) // moves the player acc to die's value
{
int x,y;
if(player == 1)
{
x = p[0][0];
y = p[0][1];
}else
{
x = p[1][0];
y = p[1][1];
}
y = (y+14+die)%7;
x = x + (y+die)/7;
if(x<0)
{
x = 0;
y = 0;
}
p[player-1][0] = x;
p[player-1][1] = y;
if(x>6)
{
p[player-1][0] = 6;
p[player-1][1] = 6;
return;
}
switch(map[x][y])
{
case 3: // good block
P(map);
y = (y+2)%7;
x = x + (y+2)/7;
System.out.println("Player " + player + " stepped on a good block. He now moves 2 steps forward.");
break;
case 4: // bad block
P(map);
y = (y-2)%7;
x = x + (y-2)/7;
System.out.println("Player " + player + " stepped on a bad block. He now moves 2 steps backward.");
break;
}
p[player-1][0] = x;
p[player-1][1] = y;
}
public static void winner(int [][] map) // determines who the winner is and prints it
{
int i;
if(p[0][0]==6&&p[0][1]==6)
i = 1;
else i = 2;
System.out.println(" The winner of this game is Player " + i);
}
public static void main (String[] args) throws java.lang.Exception
{
int[][] map = new int[7][7]; //initializing the map of the game
int i;
int j;
map = init(map); // map of the game with 3 being good blocks and 4 being bad blocks
int die = 0;
int player = 1;
System.out.println(" 3 in the map are good blocks, 4 in the map are bad blocks ");
P(map,0);
int check = 0;
Scanner sc = new Scanner(System.in);
while((p[0][0]!=6&&p[0][1]!=6)||(p[1][0]!=6&&p[1][1]!=6)) // till one of the players reaches the finish line
// game goes on
{
switch(player)
{
case 1:
System.out.print(" Rolling a die for player 1 : ");
die = rollDie();
break;
case 2:
System.out.print(" Rolling a die for player 2 : ");
die = rollDie();
break;
}
move(player, map, die);
P(map);
player = 1 + player%2; // if player == 1, it turns it into 2
// if player == 2, it turns it into 1
System.out.print(" Enter -1 to terminate the program. Enter any other integer to continue the program. ");
check = sc.nextInt();
System.out.println(); // prints a new line
if(check == -1)
{
System.out.print(" The user terminated the program ");
return ;
}
}
winner(map);
}
}
PseudoCode :
main()
{
initiate the map
ask the user for number of good/bad blocks
while(none of the players reach the end)
{
die = random_number;
move (player,map,die);
print the map after moving
change the value of player(from 1 to 2 or 2 to 1)
ask the user whether he wants to continue the game or not
}
announce which player won
}
move(int player, int [][] map, int die)
{
if(die>0)
advance the player according to the value of dice
if(die<0)
the player comes according to the value of dice
if(any player reaches final)
exit this function
check whether the player is currently on good/bad block and move the player accordingly
}
*************NOTE*************
I wrote comments and explained to the best of my knowledge.
If there is some doubt of any kind or any mistake on my behalf, please reply in the comments.
If everything is clear, please rate accordingly. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.