Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Jul 11th, 2008, 1:43 AM   #1
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 345
Rep Power: 4 cwl157 is on a distinguished road
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)
  1.  
  2. // check for collision, if no collision set the piece
  3. static void setRedPieces(int xPos, int yPos, Board b)
  4. {
  5. // loop through row
  6. for (int row = 0; row < 3; row++)
  7. {
  8. // loop through col
  9. for (int col = 0; col < 8; col++)
  10. {
  11. // loop through red pieces
  12. for (int i = 0; i < 12; i++)
  13. {
  14. pieces[i].setXPos(col);
  15. pieces[i].setYPos(row);
  16. if (!checkCollision(pieces[i].getXPos(), pieces[i].getYPos(), b))
  17. b.getBoard()[pieces[i].getYPos()][pieces[i].getXPos()] = 3;
  18. } // end for
  19. } // end for
  20. } // end for
  21. } // end setPiece

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)
  1. // move a red piece
  2. void moveRed(int pieceToMove, Board b)
  3. {
  4. if (!checkCollision(pieces[pieceToMove].getXPos()+1, pieces[pieceToMove].getYPos()+1, b))
  5. {
  6. b.getBoard()[pieces[pieceToMove].getYPos()][pieces[pieceToMove].getXPos()] = 1;
  7. // gameBoard[yPos][xPos] = 3;
  8. b.getBoard()[pieces[pieceToMove].getYPos()+1][pieces[pieceToMove].getXPos()+1] = 3;
  9. } // end if
  10. } // end moveRed

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.
cwl157 is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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 10:12 PM
Visualizing How Game Development Would Take Place Josef_Stalin Coder's Corner Lounge 4 Mar 14th, 2008 11:52 AM
Java programmers, game developers, artists, be ware! RPG game team is recruiting! atcomputers.us Paid Job Offers 7 Sep 25th, 2005 8:25 PM
Check out my Checkers Game (C# Winforms) OpenLoop Show Off Your Open Source Projects 1 Jul 20th, 2005 12:27 PM
Programmers Needed! Online Game troy_eisert C++ 2 Jan 29th, 2005 1:51 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:53 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC