Results 1 to 5 of 5
Thread: Chess game
- 12-23-2009, 12:45 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 31
- Rep Power
- 0
Chess game
Hi guys.
I am making text based chess game [very simple] and I have managed to figure out arrays and objects. The problem is that when I display on screen, the strings are not in a 8x8 box, but all over the place. Basically if I drag the terminal horizontally the pieces will be in one horizontal line
Java Code:import java.util.*; public class chess{ public Pieces Baseboard[][]; //create a two dimensional array for the board public boolean valid; //is a move valid public String display; protected Pawn pawn; protected Castle castle; //sets up game world public void createWorld() { Baseboard = new Pieces[8][8]; for(int i = 0; i<8; i++) { for(int k= 0; k<8; k++) { Baseboard[i][k] = null; } } for(int i = 0; i<8; i++) { pawn = new Pawn(); Baseboard[1][i] = pawn.Pawn(i, 1, false); } for(int i = 0; i<8; i++) { pawn = new Pawn(); Baseboard[6][i] = pawn.Pawn(i, 6, true); } castle = new Castle(); Baseboard[0][0] = castle.Castle(0, 0, false); castle = new Castle(); Baseboard[7][7] = castle.Castle(7, 7, true); //before print for(int i = 0; i<8; i++) { for(int k = 0; k<8;k++) { if (Baseboard[i][k]!= null) { System.out.print(Baseboard[i][k].toString()+" "); //toString //displays the type and position of a piece } else { System.out.print(" Empty "); } } } } void makemove (/*user input*/){ //the 1s can be subbed for user values and the 2,1 is the new cords (x,y) //the method valid moves checks to see if move is legal Baseboard[1][1].validmoves (2,1); } public static void main (String args[]){ // creating instance of game chess game = new chess(); //creating game world game.createWorld(); game.makemove(); //while game running //while(true){ //ask for moves //check validation //move piece //until game over, break //} } }
If you can help I would be greatfull
-
You may wish to add a System.out.println() statement in the nested for loop, in the outer loop, just beneath the inner loop.
Do you understand why you need to do this?Java Code:for(int i = 0; i<8; i++) { for(int k = 0; k<8;k++) { if (Baseboard[i][k]!= null) { System.out.print(Baseboard[i][k].toString()+" "); } else { System.out.print(" Empty "); } } System.out.println(); // **** here }
- 12-23-2009, 05:40 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 31
- Rep Power
- 0
No, I don't know why it works this way. However I tried it and the "empty" squares stayed fixed but it still prints all over the place.
It looks sth like this
Class type Castle 0,0 Empty Empty Empty Empty Empty Empty Empty Class type Pawn 0,1 Class type Pawn 1,1 Class type Pawn 2,1 Class type Pawn 3,1 Class type Pawn 4,1 Class type Pawn 5,1 Class type Pawn 6,1 Class type Pawn 7,1 Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Empty Class type Pawn 0,6 Class type Pawn 1,6 Class type Pawn 2,6 Class type Pawn 3,6 Class type Pawn 4,6 Class type Pawn 5,6 Class type Pawn 6,6 Class type Pawn 7,6 Empty Empty Empty Empty Empty Empty Empty Class type Castle 7,7 sddssds
Thanks for thatLast edited by michail; 12-23-2009 at 05:44 PM. Reason: Answered myself!
- 12-23-2009, 06:00 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Check your 'toString()' methods in your Pawn, Castle etc. classes; they don't print as you expected; b.t.w. you really do need that System.out.println() call as was suggested.
kind regards,
Jos
- 12-23-2009, 06:51 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 31
- Rep Power
- 0
Similar Threads
-
a good java chess program
By rico16135 in forum New To JavaReplies: 10Last Post: 11-06-2010, 12:25 PM -
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 04:49 AM -
I want play chess in Java
By ganzorig in forum Advanced JavaReplies: 6Last Post: 11-15-2009, 04:53 AM -
New Chess-Like game
By jSarK0Y in forum Reviews / AdvertisingReplies: 3Last Post: 06-10-2009, 03:28 AM -
2D strategy game or 2D war game
By led1433 in forum Java 2DReplies: 5Last Post: 02-10-2009, 06:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks