Question

In: Computer Science

**NEED ACTUAL JAVA CODE** Basically this code is defining the board for the game tigers and...

**NEED ACTUAL JAVA CODE**

Basically this code is defining the board for the game tigers and goats or 'Bagh chan'.

Ive posted the other 3 classes that I need help with, didnt want to put all 4 in one question.


public class GameViewer implements MouseListener
{
// instance variables
private int bkSize; // block size - all other measurements to be derived from bkSize
private int brdSize; // board size
private SimpleCanvas sc; // an object of SimpleCanvas to draw
private GameRules rules; // an object of GameRules
private Board bd; // an object of Board
private AIplayer ai; //an object of AIplayer
  
// 2D coordinates of valid locations on the board in steps of block size
public static final int[][] locs = {{1,1},{1,4},{1,7},{4,7},{7,7},{7,4},{7,1},{4,1},
{2,2},{2,4},{2,6},{4,6},{6,6},{6,4},{6,2},{4,2},
{3,3},{3,4},{3,5},{4,5},{5,5},{5,4},{5,3},{4,3}};
// source and destination for the goat moves   
private int[] mov = {-1,-1}; //-1 means no selection

/**
* Constructor for objects of class GameViewer
* Initializes instance variables and adds mouse listener.
* Draws the board.
*/
public GameViewer(int bkSize)
{
this.bkSize = bkSize;
brdSize = bkSize*8;
sc = new SimpleCanvas("Tigers and Goats", brdSize, brdSize, Color.BLUE);
sc.addMouseListener(this);   
rules = new GameRules();
bd = new Board();
ai = new AIplayer();
drawBoard();
}
  
/**
* Constructor with default block size
*/
public GameViewer( )
{
this(80);
}
  
/**
* Draws the boad lines and the pieces as per their locations.
* Drawing of lines is provided, students to implement drawing
* of pieces and number of goats.
*/
private void drawBoard()
{
sc.drawRectangle(0,0,brdSize,brdSize,Color.BLUE); //wipe the canvas
  
//draw shadows of Goats and Tigers - not compulsory //////////////////////
  
// Draw the lines
for(int i=1; i<9; i++)
{
//diagonal and middle line
sc.drawLine(locs[i-1][0]*bkSize, locs[i-1][1]*bkSize,
locs[i+15][0]*bkSize, locs[i+15][1]*bkSize, Color.red);
if(i==4 || i==8) continue; //no more to draw at i=4,8
// vertical line
sc.drawLine(i*bkSize, i*bkSize,
i*bkSize, brdSize-i*bkSize,Color.white);
// horizontal line
sc.drawLine(i*bkSize, i*bkSize,
brdSize-i*bkSize, i*bkSize, Color.white);
  
}
  
// TODO 10
// Draw the goats and tigers. (Drawing the shadows is not compulsory)
// Display the number of goats
  
}
  
/**
* If vacant, place a goat at the user clicked location on board.
* Update goat count in rules and draw the updated board
*/
public void placeGoat(int loc)
{   
//TODO 2
}
  
/**
* Calls the placeTiger method of AIplayer to place a tiger on the board.
* Increments tiger count in rules.
* Draws the updated board.
*/
public void placeTiger()
{   
//TODO 13
  
}
  
/**
* Toggles goat selection - changes the colour of selected goat.
* Resets selection and changes the colour back when the same goat is clicked again.
* Selects destination (if vacant) to move and calls moveGoat to make the move.
*/
public void selectGoatMove(int loc)
{   
//TODO 16
  
}
  
/**
* Make the user selected goat move only if legal otherwise set the destination to -1 (invalid).
* If did make a goat move, then update board, draw the updated board, reset mov to -1,-1.
* and call tigersMove() since after every goat move, there is a tiger move.
*/
public void moveGoat()
{   
//TODO 18
  
}
/**
* Call AIplayer to make its move. Update and draw the board after the move.
* If Tigers cannot move, display "Goats Win!".
* If goats are less than 6, display "Tigers Win!".
* No need to terminate the game.
*/
public void tigersMove()
{
//TODO 20
}
  
/**
* Respond to a mouse click on the board.
* Get a valid location nearest to the click (from GameRules).
* If nearest location is still far, do nothing.
* Otherwise, call placeGoat to place a goat at the location.
* Call this.placeTiger when it is the tigers turn to place.
* When the game changes to move stage, call selectGoatMove to move
* the user selected goat to the user selected destination.
*/
public void mousePressed(MouseEvent e)
{
//TODO 1
  
}
  
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}

this is the gameviewer class now board class, sorry.

Solutions

Expert Solution

Note :- This is the main algorithem for the game Thanks.

ALGORITHM The game board consist 23 spots named as B0 to B22. Human player can choose a role as tiger/goat. Assume that the humanplayer is playing goat as G and the Android is playing tiger as T. A. Check for winning condition At the start of its turn, the Android first checks for tigers winning cases and goats winning cases. For the tiger’s winning, the number of G in the board is less than or equal five. For the goat’s winning, the moves for all three T are null. Algorithm: Let B[i] be the ith spot. So I lies between 0 and 22. Let numerically T be represented by 0 and G by 1 and empty spot by 2.

value=0
For (int i=0; i<22; i++) {
If(i==0) { //Tiger blocked in spot B0
If(B[i] == 0 && B[i+2] == 1 && B[i+3] == 1 &&
B[i+4] == 1 && B[i+5] == 1 && B[i+8] == 1 &&
B[i+9] == 1 && B[i+10] == 1 && B[i+11] == 1) {
value=value+1
}
}
If(i==1) { //Tiger blocked in spot B1
If(B[i] == 0 && B[i+1] == 1 && B[i+2] == 1 &&
B[i+6] == 1 && B[i+12] == 1) {
value=value+1
}
}
If(i==2) { //Tiger blocked in spot B2
If(B[i] == 0 && B[i+1] == 1 && B[i+2] == 1 &&
B[i-1] == 1 && B[i-2] == 1 && B[i+6] == 1 &&
B[i+12] == 1) {
value=value+1
}
}
If (i==3 || i==4) { //Tiger blocked in spot B3 or B4
If(B[i] == 0 && B[i+1] == 1 && B[i-1] == 1 &&
B[i+2] == 1 && B[i-2] == 1 && B[i+6] == 1 &&
B[i+12] == 1 && B[i-i] == 1) {
value=value+1
}
}
If(i==5) { //Tiger blocked in spot B5
If(B[i] == 0 && B[i+1] == 1 && B[i-1] == 1 && B[i-2]
== 1 && B[i+6] == 1 && B[i+12] == 1) {
value=value+1
}
}
If(i==6) { //Tiger blocked in spot B6
If(B[i] == 0 && B[i-1] == 1 && B[i-2] == 1 && B[i+6]
== 1 && B[i+12] == 1) {
value = value+1
}
}
If(i==7) { //Tiger blocked in spot B7
If(B[i] == 0 && B[i-6] == 1 && B[i+6] == 1 &&
B[i+1] == 1 && B[i+2] == 1) {
value =value+1
}}
If(i==8) { //Tiger blocked in spot B8
If(B[i] == 0 && B[i-1] == 1 && B[i+1] == 1 &&
B[i+2] == 1 && B[i-6] == 1 && B[i-i] == 1 && B[i+6]
== 1 && B[i+11] == 1) {
value= value+1
}
}
If (i==9 || i==10) { //Tiger blocked in spot B9 or B10
If(B[i] == 0 && B[i-1] == 1 && B[i-2] == 1 && B[i+1]
== 1 && B[i+2] == 1 && B[i-6] == 1 && B[i-i] == 1
&& B[i+6] == 1 && B[i+11] == 1) {
value=value+1
}
}
If(i==11) { //Tiger blocked in spot B11
If(B[i] == 0 && B[i-1] == 1 && B[i+1] == 1 && B[i-2]
== 1 && B[i-6] == 1 && B[i-i] == 1 && B[i+6] == 1
&& B[i+11] == 1) {
value=value+1
}
}
If (i==12) { //Tiger blocked in spot B12
If(B[i] == 0 && B[i-6] == 1 && B[i+6] == 1 && B[i-1]
== 1 && B[i-2] == 1) {
value=value+1
}
}
If (i==13) { //Tiger blocked in spot B13
If(B[i] == 0 && B[i+1] == 1 && B[i+2] == 1 &&
B[i-6] == 1 && B[i-12] == 1) {
value=value+1
}
}
If (i==14) { //Tiger blocked in spot B14
If(B[i] == 0 && B[i-1] == 1 && B[i+1] == 1 &&
B[i+2] == 1 && B[i-6] == 1 && B[i-12] == 1 &&
B[i+5] == 1) {
value=value+1
}
}
If (i==15 || i==16) { //Tiger blocked in spot B15 or 16
If(B[i] == 0 && B[i-1] == 1 && B[i-2] == 1 && B[i+1]
== 1 && B[i+2] == 1 && B[i-6] == 1 && B[i-12] == 1
&& B[i+5] == 1) {
value=value+1
}
}
If (i==17) { //Tiger blocked in spot B17
If(B[i] == 0 && B[i-1] == 1 && B[i+1] == 1 &&
B[i-2] == 1 && B[i-6] == 1 && B[i-12] == 1 &&
B[i+5] == 1) {
value=value+1
}
}
If (i==18) { //Tiger blocked in spot B18
If(B[i] == 0 && B[i-1] == 1 && B[i-2] == 1 && B[i-6]
== 1 && B[i-12] == 1) {
value=value+1
}
}
If(i==19) { //Tiger blocked in spot B19
If(B[i] == 0 && B[i+1] == 1 && B[i+2] == 1 &&
B[i-5] == 1 && B[i-11] == 1) {
value=value+1
}
}
If (i==20) { //Tiger blocked in spot B20
If(B[i] == 0 && B[i-1] == 1 && B[i-5] == 1 && B[i+1]
== 1 && B[i+2] == 1 && B[i-11] == 1) {
value=value+1
}
}
If (i==21) { //Tiger blocked in spot B21
If(B[i] == 0 && B[i-1] == 1 && B[i-5] == 1 && B[i+1]
== 1 && B[i-2] == 1 && B[i-11] == 1) {
value=value+1
}
}
If (i==22) { //Tiger blocked in spot B22
If(B[i] == 0 && B[i-1] == 1 && B[i-2] == 1 && B[i-5]
== 1 && B[i-11] == 1) {
value=value+1
}
}
}

Playing for Defensive Whenever the Android detects an empty spot which is followed by T, G it may insert T in the empty spot or capture the G to prevent the blocking from G.

Moves for Winning First, it is obvious that the goats have to consider the border during the placement phase - any goat that strays into the center will get eaten, or cause the demise of some other goat, without any apparent benefit in return for the sacrifice. Therefore, goatplayer strategy sounds simple: first populate the borders, and when at full strength, try to advance in unbroken formation, in the hope of suffocating the tigers. Second, tiger is moving back and forth, until near the end of the placement phase. Their goal is to stay far apart from each other, for two reasons: (1) in order to probe the full length of the goats’ front line for gaps, (2) so as to make it hard for the goats to immobilize all the three tigers at the same time.

Capture the Goats For the tigers, the objective is to ‘capture’ ten goats to win. Capturing is performed by jumping over the goats, although capturing is not obligatory. There are possible cases to capture the goat. Algorithm: Let B[i] be the ith spot. So I lies between 0 and 22. Let numerically T be represented by 0 and G by 1 and empty spot by 2.

If (i==0 && B[i]==0) {
If(B[i+2] == 1 && B[i+8] == 2) {
B[i]=2, B[i+2] =2, B[i+8] =0
}
If (B[i+3] == 1 && B[i+9] == 2) {
B[i]=2, B[i+3] =2, B[i+9] =0
}
If (B[i+4] ==1 && B[i+10] ==2) {
B[i]=2, B[i+4] =2, B[i+10] =0
}
If (B[i+5] == 1 && B[i+11] == 2) {
B[i]=2, B[i+5] =2, B[i+11] =0
}
}
If (i==1 || i==2 && B[i]==0) {
If(B[i+1] == 1 && B[i+2] == 2) {
B[i]=2, B[i+1] =2, B[i+2] =0
}
If (B[i+6] == 1 && B[i+12] == 2) {
B[i]=2, B[i+6] =2, B[i+12] =0
}
}
If (i==3 || i==4 && B[i]==0) {
If(B[i+1] == 1 && B[i+2] == 2) {
B[i]=2, B[i+1] =2, B[i+2] =0
}
If (B[i+6] == 1 && B[i+12] == 2) {
B[i]=2, B[i+6] =2, B[i+12] =0
}
If (B[i-1] == 1 && B[i-2] == 2) {
B[i]=2, B[i-1] =2, B[i-2] =0
}
}
If (i==5 || i==6 && B[i]==0) {
If(B[i-1] == 1 && B[i-2] == 2) {
B[i]=2, B[i-1] =2, B[i-2] =0
}
If (B[i+6] == 1 && B[i+12] == 2) {
B[i]=2, B[i+6] =2, B[i+12] =0
}
}
If (i==7 && B[i]==0) {
If(B[i+1] == 1 && B[i+2] == 2) {
B[i]=2, B[i+1] =2, B[i+2] =0
}
}
If (i==8 && B[i]==0) {
If(B[i+1] == 1 && B[i+2] == 2) {
B[i]=2, B[i+1] =2, B[i+2] =0
}
If (B[i+6] == 1 && B[i+11] == 2) {
B[i]=2, B[i+6] =2, B[i+11] =0
}
If (B[i-6] == 1 && B[i-i] == 2) {
B[i]=2, B[i-6] =2, B[i-i] =0
}
}
If (i==9 || i==10 && B[i]==0) {
If(B[i+1] == 1 && B[i+2] == 2) {
B[i]=2, B[i+1] =2, B[i+2] =0
}
If(B[i-1] == 1 && B[i-2] == 2) {
B[i]=2, B[i-1] =2, B[i-2] =0
}
If (B[i+6] == 1 && B[i+11] == 2) {
B[i]=2, B[i+6] =2, B[i+11] =0
}
If (B[i-6] == 1 && B[i-i] == 2) {
B[i]=2, B[i-6] =2, B[i-i] =0
}
}
If (i==11 && B[i]==0) {
If(B[i-1] == 1 && B[i-2] == 2) {
B[i]=2, B[i+1] =2, B[i+2] =0
}
If (B[i+6] == 1 && B[i+11] == 2) {
B[i]=2, B[i+6] =2, B[i+11] =0
}
If (B[i-6] == 1 && B[i-i] == 2) {
B[i]=2, B[i-6] =2, B[i-i] =0
}
}
If (i==12 && B[i]==0) {
If(B[i-1] == 1 && B[i-2] == 2) {
B[i]=2, B[i-1] =2, B[i-2] =0
}
}
If (i==13 || i==14 && B[i]==0) {
If(B[i+1] == 1 && B[i+2] == 2) {
B[i]=2, B[i+1] =2, B[i+2] =0
}
If (B[i-6] == 1 && B[i-12] == 2) {
B[i]=2, B[i-6] =2, B[i-12] =0
}
}
If (i==15 || i==16 && B[i]==0) {
If(B[i+1] == 1 && B[i+2] == 2) {
B[i]=2, B[i+1] =2, B[i+2] =0
}
If (B[i-6] == 1 && B[i-12] == 2) {
B[i]=2, B[i-6] =2, B[i-12] =0
}
If (B[i-1] == 1 && B[i-2] == 2) {
B[i]=2, B[i-1] =2, B[i-2] =0
}
}
If (i==17 || i==18 && B[i]==0) {
If(B[i-1] == 1 && B[i-2] == 2) {
B[i]=2, B[i-1] =2, B[i-2] =0
}
If (B[i-6] == 1 && B[i-12] == 2) {
B[i]=2, B[i-6] =2, B[i-12] =0
}
}
If (i==19 || i==20 && B[i]==0) {
If(B[i+1] == 1 && B[i+2] == 2) {
B[i]=2, B[i+1] =2, B[i+2] =0
}
If (B[i-5] == 1 && B[i-11] == 2) {
B[i]=2, B[i-5] =2, B[i-11] =0
}
}
If (i==21 || i==22 && B[i]==0) {
If(B[i-1] == 1 && B[i-2] == 2) {
B[i]=2, B[i-1] =2, B[i-2] =0
}
If (B[i-5] == 1 && B[i-11] == 2) {
B[i]=2, B[i-5] =2, B[i-11] =0
}
}

Thanks, Please appreciate my hard work. :-(


Related Solutions

**NEED ACTUAL JAVA CODE** Basically this code is defining the board for the game tigers and...
**NEED ACTUAL JAVA CODE** Basically this code is defining the board for the game tigers and goats or 'Bagh chan'. Ive posted the other 3 classes that I need help with, didnt want to put all 4 in one question. /** * Maintains and updates the status of the board * i.e. the locations of goats and tigers * * @Student 1 Name: * @Student 1 Number: * * @Student 2 Name: * @Student 2 Number: */ public class Board...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
Need to fix this code for tc -tac-toe game .. see the code below and fix...
Need to fix this code for tc -tac-toe game .. see the code below and fix it #include <iostream> using namespace std; void display_board(); void player_turn(); bool gameover (); char turn ; bool draw = false; char board [3][3] = { {'1', '2', '3'}, { '4', '5', '6'}, { '7', '8', '9'}}; int main() { cout << " Lets play Tc- Tac- toe game " <<endl ; cout << " Player 1 [X] ----- player 2 [0] " <<endl <<endl;...
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
Could we turn this java code into a GUI game? Add a orange and a green...
Could we turn this java code into a GUI game? Add a orange and a green trophy object. Orange trophy costs 15 clicks and every time we buy a orange trophy price increases by * 1.09. Green trophy costs 100 clicks and every time we buy a green trophy price increases by * 1.09. Example: you would click the button 15 times in order to be allowed to buy the orange trophy then it resets but then the cost becomes...
Could we turn this java code into a GUI game? Add a orange and a green...
Could we turn this java code into a GUI game? Add a orange and a green trophy object. Orange trophy costs 15 clicks and every time we buy a orange trophy price increases by * 1.09. Green trophy costs 100 clicks and every time we buy a green trophy price increases by * 1.09. Example: you would click the button 15 times in order to be allowed to buy the orange trophy then it resets but then the cost becomes...
Write a class encapsulating a board game. A board game has the following additional attributes: the...
Write a class encapsulating a board game. A board game has the following additional attributes: the number of players and whether the game can end in a tie. Code the constructor and the toString method of the new class. You also need to include a client class(with the main method) to test your code. code in Python
Hi! I need it in android studio and in java Design a game app “BouncingBall ”...
Hi! I need it in android studio and in java Design a game app “BouncingBall ” in which the user’s goal is to prevent a bouncing ball from falling off the bottom of the screen. When the user presses the start button, a ball bounces off the top, left and right sides (the “walls”) of the screen. A horizontal bar on the bottom of the screen serves as a paddle to prevent the ball from hitting the bottom of the...
Hi! I need it in android studio and in java Design a game app “BouncingBall ”...
Hi! I need it in android studio and in java Design a game app “BouncingBall ” in which the user’s goal is to prevent a bouncing ball from falling off the bottom of the screen. When the user presses the start button, a ball bounces off the top, left and right sides (the “walls”) of the screen. A horizontal bar on the bottom of the screen serves as a paddle to prevent the ball from hitting the bottom of the...
I need a Verilog code that makes the LEDs on the FPGA board works like this....
I need a Verilog code that makes the LEDs on the FPGA board works like this. https://image.ibb.co/mu5tnS/6.gif There are 16 LEDs in the FPGA board
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT