Can't make a 9x9 array for a board game

i'm using a past prog that had a 5x5 array but can't get it to

Page 1 of 1

1 Replies - 5465 Views - Last Post: 06 April 2010 - 09:21 AM Rate Topic: -----

#1 Guest_Bryan*


Reputation:

Can't make a 9x9 array for a board game

Posted 06 April 2010 - 09:07 AM

this is a recreation of the old computer game Quoridor. I'm using the template from an old project i had, and it had a 5x5 board, but not using an array. So now i need to implement an array to have a 9x9 board...Thanks for any help



/**
 * class Board
 * 		Implement the functionality for a location-guessing game.  
 * The program displays a grid of 25 circles on the screen.  
 * Selecting the "Randomize" button changes the colors of the
 * circles and increments the move number.  The user can then
 * enlist someone to click on a single circle to change its 
 * color (while they are not watching of course).  Then the user
 * identifies which circle it was by again clicking on the 
 * same circle.  
 * 
 * If the user succeeds, a counter is updated and displayed.
 * 
 * The secret is to use a parity check in both row and column.
 * The "move number" determines if parity is even or odd.  By
 * observing the row and column with changed parity, the user
 * can correctly identify which circle was changed.
 * 
 * Class: CS 107, Spring 2010
 * Lab: Min Shen, Tues. 8:00 AM
 * System: BlueJ 2.1.3, jsdk 1.6, Windows XP

 * @author Bryan Gehrling 
 * @version April 9, 2010
 * 
 */
//rotate
import java.util.Scanner;		  // allows user input from the keyboard
import java.util.Random;          // allows getting a random number


public class Board
{      	
    // Create the random number generator.  "Seed" this with the value 1.
    // Removing the value from the parenthesis will give a different result every
    // time, which isn't what we want when we are debugging.
    Random randomNumberGenerator = new Random(1);  
	
	// declare buttons to be displayed
    Square rotateButton; 		// rotate selected wall
    Square exitButton;			// users presses this to exit program
    Square gameMessagesLabel;	// Used to display messages
    
    // constants
    final static int LENGTH_OF_SIDE = 5;
    final static int NUMBER_OF_SQUARES = LENGTH_OF_SIDE * LENGTH_OF_SIDE;
    final static int SQUARE_SIZE = 40;
    final int OFFSET = 10;	

    
    // declare 25 squares to be displayed on the screen, in a 9 x 9 array
    Square[] theSquares = new Square[ NUMBER_OF_SQUARES];

    
    // declare a rectangle, to illustrate rotating and moving it
    MyRectangle aRectangle;
    // Constructor to create the board
    
    Board()
    {      
  Circle c0, c1;//declares two pawns
  
        c0 = new Circle();
        // change the values and make it visible
        c0.setPosition( 155, 55);
        c0.setDiameter( 40);
        c0.setColor( "white");
        c0.setLabel( "s" + 2);   // will be "s2" 
        c0.makeVisible();
        
        c1 = new Circle();
        // change the values and make it visible
        c1.setPosition( 105, 55);
        c1.setDiameter( 40);
        c1.setColor( "black");
        c1.setLabel( "s" + 1);   // will be "s1" 
        c1.makeVisible();
        
        
        
        int x=50;
        for (int i=0;i<9;i++){
        for (int j=0;j<9;j++){
                theSquares[i][j]=new Square(x*(j)+50,x*(i)+50, 200/size,"gray",true,"");
                
            }
        }
    	// Set variables used to indicate upper-left position of object to be drawn.
    	// This way the entire board can be moved by simply changing these values.
    	int xPosition = 40; 
    	int yPosition = 40;
    	
		// make the board background
    	Square boardBackground = null;		// make a Square reference that we can reuse
    	boardBackground = new Square (xPosition, yPosition, 270, "black", true, "");  // background large black outline
    	xPosition += OFFSET;		// same as xPosition = xPosition + OFFSET;    // add OFFSET to x
    	yPosition += OFFSET;		// same as yPosition = yPosition + OFFSET;    // add OFFSET to y
		boardBackground = new Square (xPosition, yPosition, 250, "lightGray", true, "");	// lightGray board background
		
		// declare a Circle reference, which can point to any circle (s0, s1, etc), like a synonym
		Square theSquare = null;	

		// move the starting position for x,y up and to the left by half the OFFSET + some small amount
    	xPosition = xPosition - OFFSET/2;	   
    	yPosition = xPosition - OFFSET/4 + 2;  

		// draw the squares as a 5 x 5 array
		for(int i=0; i<NUMBER_OF_SQUARES; i++) {
			// Allocate memory and set the values for this square
			theSquares[ i] = new Square( xPosition + OFFSET, yPosition  + OFFSET, 
										 SQUARE_SIZE,
									     "darkGray", true, "");
			
			// increment x,y position in preparation for next time.  Check to see if we're at the end of a row
			if( (i+1)%5 == 0) {
				// end of row, so reset xPosition to the left column, and yPosition to next row
				xPosition = xPosition - ((SQUARE_SIZE + OFFSET) * 4);
				yPosition = yPosition + SQUARE_SIZE + OFFSET;
			}
			else {
				// default, increment the column only
				xPosition = xPosition + SQUARE_SIZE + OFFSET;
			}
		}//end for( int i...
		    
		// create the "Redo" and "Exit" squares near the bottom right of the board
		gameMessagesLabel = new Square( 130, 350, 40, "white", true, "Click Rotate to rotate wall *PAWN*.");
		rotateButton = new Square( 250, 350, 45, "lightGray", true, "Rotate");
		exitButton = new Square( 300, 350, 40, "lightGray", true, "  Exit");
		
		// display a rectangle
		aRectangle = new MyRectangle(180, 350, OFFSET, SQUARE_SIZE*2+OFFSET, "red", true, "");


    }//end method doIt()

    
    // Handle each mouse click.  This code should call other methods that you write.
    void handleMouseClick(int mouseX, int mouseY)
    {   
    	// Find out if the mouse was in the "Exit" button
    	if( (mouseX >= exitButton.getX()) && (mouseX <= (exitButton.getX() + exitButton.getSize())) &&   
    		(mouseY >= exitButton.getY()) && (mouseY <= (exitButton.getY() + exitButton.getSize()))
    	  ) {
    		// For debugging, uncomment the following line.  Found the square, so print it's label
    		System.out.println("Clicked on Square: " + exitButton.getLabel());
    		// Since the "Exit" square was clicked, exit the program
    		System.out.println("Exiting the program...");
    		System.exit( 0);	// exit the program
    	} 
    	// Find out if the mouse was in the "Rotate" button
    	else if( (mouseX >= rotateButton.getX()) && (mouseX <= (rotateButton.getX() + rotateButton.getSize())) &&   
        	(mouseY >= rotateButton.getY()) && (mouseY <= (rotateButton.getY() + rotateButton.getSize()))
          ) {
       		// Call the code to rotate previously selected piece
       		rotateRectangle();
       	}
       	
    	else { 
    		// Assume you clicked in the board.  Move the rectangle to this position.
	       	// First find out how much you must move in each direction
	       	int xDifference = aRectangle.getXPosition() - mouseX;
	       	int yDifference = aRectangle.getYPosition() - mouseY;
	       	// Now do a slow move in each of these directions
	       	aRectangle.slowMoveHorizontal( -xDifference);
	       	aRectangle.slowMoveVertical( -yDifference);
    	}
        
    }//end handleMouseClick(...)
    
    
    private void rotateRectangle()
    {
    	// find out which rectangle was selected
    	// For now this is easy, since there is only one
    	MyRectangle theRectangle = aRectangle;
    	
    	// store the width, then use this to swap the width and the height.
    	// You will want to adjust this code so that the rectangle appears to rotate "in place"
    	int oldWidth = theRectangle.getWidth();
    	theRectangle.setWidth( theRectangle.getHeight() );
    	theRectangle.setHeight( oldWidth);
    }
    
    
}// end class Board()




Is This A Good Question/Topic? 0

Replies To: Can't make a 9x9 array for a board game

#2 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Can't make a 9x9 array for a board game

Posted 06 April 2010 - 09:21 AM

The best way to do this is with a two dimensional array, so you're gonna have to adjust your code:

//put this where you declare all the variables
final int WIDTH = 9, HEIGHT = 9;
int/*or whatever type you want it*/[][] board = new int[WIDTH][HEIGHT]

for(int i =0; i < WIDTH; i ++){
     for(int j =0; j < HEIGHT; j++){
          //this runs through one row at a time,
          //running through every column in that row
     }
}



basically, you can store values in any position of the board then check for a win condition by using the above nested for loop and some if statements.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1