![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 333
Rep Power: 4
![]() |
checkers game
I want to make a 2 player checkers game. I am starting in text but i want to add graphics later. I have 3 classes so far piece board and player. I represent the board with a 2d int array and i was thinking then that a red piece is a 3 and a black piece is a 4 and the empty spots that are movable are 1's and the other ones are 2. So then to add graphics i just go through the game board and draw whatever needs to be drawn based on if its a 1, 2, 3, or 4. I have the board set up and the pieces on the board. I am having trouble moving them around though. My player class gets an array of pieces. The color of these pieces depends on if the person is black or red. I need a move function that looks 1 diagnol from the current x and y and detects a collision or not. If no collision it would move the index in the piece array from the old position to the new one. I get an array index out of bounds. Basically i think i don't know which index of the pieces array i would pass it. Here is how i initially set the red pieces on the board.
java Syntax (Toggle Plain Text)
So can i assume that from 0,0 in the game board array is 0 in the pieces array and so on like that? If so i have my move method: java Syntax (Toggle Plain Text)
I get an array index out of bounds exception around the check collision part. I pass it in 1 which should not be allowed to move because it should be the 2nd piece on the board if i am thinking through this correctly but i should not get the arrayIndexOutOfBounds exception. If i need to post more code i can. Thanks. |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 333
Rep Power: 4
![]() |
Re: checkers game
it is saying the x and y positions of the pieces[pieceToMove] is 7 for some reason. It appears this gets added right in that method. I changed it to just place 1 red piece on the board at 0,0 and that works fine. But then the x and y values get changed to 7 and 7 also if i have a get method that gets the xPos and i say
pieces[pieceToMove].getXPos()+1 to take the current x position and see if the next 1 is available it sets it to 71 it just concatinates the 1 onto the end of the 7 and does not check for 1 + 7 which would be 8 but it should not even be 7 it should be 1 because the pieces[pieceToMove] x and y pos should be 0. It should be the first piece. I am just going to post my whole Player class where all this happens so you can see the whole thing.
[code = "java"]
import java.awt.*;
import javax.swing.*;
public class Player
{
static Piece[] pieces = new Piece[12];
// static Board b = new Board();
String name;
boolean isRed;
// constructor,
// tell if the person is black or red and make the correct array for them
// put pieces on board
public Player(String newName, String color, Board b)
{
name = newName;
if (color.equalsIgnoreCase("red"))
{
// initialize the RedPiece array
for (int i = 0; i < 12; i++)
{
pieces[i] = new Piece(Color.RED);
} // end for
//setRedPieces(0,0, b);
System.out.println("inside constructor pieces[0].getXPos is " + pieces[0].getXPos());
System.out.println("inside constructor pieces[0].getYPos is " + pieces[0].getYPos());
setOneRedPiece(pieces[0].getXPos(),pieces[0].getYPos(), b);
} // end if
else if (color.equalsIgnoreCase("black"))
{
// initialize a black piece array
for (int i = 0; i < 12; i++)
{
pieces[i] = new Piece(Color.BLACK);
} // end for
setBlackPieces(5, 0, b);
} // end else if
} // end PlayGame constructor
// return true if there is a collision
static boolean checkCollision(int xPos, int yPos, Board b)
{
// check if you are on an even row
if (yPos %2 == 0)
{
// this means you are not on a gray square
if (b.getBoard()[yPos][xPos] != 1)
return true;
} // end if
// check if you are on an odd row
else if (yPos %2 == 1)
{
// this means you are not on a gray square
if (b.getBoard()[yPos][xPos] != 1)
return true;
} // end else if
return false;
} // end checkCollision
/*
// check for collision, if no collision set the piece
static void setRedPieces(int xPos, int yPos, Board b)
{
// loop through red pieces
for (int i = 0; i < 12; i++)
{
// loop through row
for (int row = 0; row < 3; row++)
{
// loop through col
for (int col = 0; col < 8; col++)
{
pieces[i].setXPos(col);
pieces[i].setYPos(row);
if (!checkCollision(pieces[i].getXPos(), pieces[i].getYPos(), b))
b.getBoard()[pieces[i].getYPos()][pieces[i].getXPos()] = 3;
} // end for
} // end for
} // end for
} // end setPiece
*/
// set one piece
static void setOneRedPiece(int xPos, int yPos, Board b)
{
b.getBoard()[pieces[0].getYPos()][pieces[0].getXPos()] = 3;
} // end setOneRedPiece
// check for collision, if no collision set the piece
static void setBlackPieces(int xPos, int yPos, Board b)
{
// loop through red pieces
for (int i = 0; i < 12; i++)
{
// loop through row
for (int row = 5; row < 8; row++)
{
// loop through col
for (int col = 0; col < 8; col++)
{
pieces[i].setXPos(col);
pieces[i].setYPos(row);
if (!checkCollision(pieces[i].getXPos(), pieces[i].getYPos(), b))
b.getBoard()[pieces[i].getYPos()][pieces[i].getXPos()] = 4;
} // end for
} // end for
} // end for
} // end setPiece
/*
// try to move, move back if you can't
boolean tryToMove(int pieceToMove, Board b)
{
pieces[pieceToMove].setXPos(pieces[pieceToMove].getXPos()+1);
pieces[pieceToMove].setYPos(pieces[pieceToMove].getYPos()+1);
if (checkCollision(pieces[pieceToMove].getXPos(), pieces[pieceToMove].getYPos(), b))
{
pieces[pieceToMove].setXPos(pieces[pieceToMove].getXPos()-1);
pieces[pieceToMove].setYPos(pieces[pieceToMove].getYPos()-1);
return false;
} // end if
return true;
} // end tryToMove
**/
// move a red piece
void moveRed(int pieceToMove, Board b)
{
System.out.println("the piece to move is " + pieceToMove);
System.out.println("the x pos of pieces[pieceToMove] is " + pieces[pieceToMove].xPos+1);
System.out.println("the Y pos of pieces[pieceToMove] is " + pieces[pieceToMove].yPos+1);
if (!checkCollision(pieces[pieceToMove].xPos+1, pieces[pieceToMove].getYPos()+1, b))
{
b.getBoard()[pieces[pieceToMove].getYPos()][pieces[pieceToMove].getXPos()] = 1;
// gameBoard[yPos][xPos] = 3;
pieces[pieceToMove].setYPos(pieces[pieceToMove].getYPos()+1);
pieces[pieceToMove].setXPos(pieces[pieceToMove].getXPos()+1);
b.getBoard()[pieces[pieceToMove].getYPos()][pieces[pieceToMove].getXPos()] = 3;
} // end if
} // end moveRed
// move a black piece
void moveBlack(int pieceToMove, Board b)
{
b.getBoard()[pieces[pieceToMove].getYPos()-1][pieces[pieceToMove].getXPos()-1] = 1;
// gameBoard[yPos][xPos] = 3;
b.getBoard()[pieces[pieceToMove].getYPos()][pieces[pieceToMove].getXPos()] = 4;
} // end moveBlack
} // end class Player |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Coder's Block Arena - The Game AI Platform | Sane | Existing Project Development | 23 | May 6th, 2008 9:12 PM |
| Visualizing How Game Development Would Take Place | Josef_Stalin | Coder's Corner Lounge | 4 | Mar 14th, 2008 10:52 AM |
| Java programmers, game developers, artists, be ware! RPG game team is recruiting! | atcomputers.us | Paid Job Offers | 7 | Sep 25th, 2005 7:25 PM |
| Check out my Checkers Game (C# Winforms) | OpenLoop | Show Off Your Open Source Projects | 1 | Jul 20th, 2005 11:27 AM |
| Programmers Needed! Online Game | troy_eisert | C++ | 2 | Jan 29th, 2005 12:51 PM |