Results 1 to 5 of 5

Thread: Chess game

  1. #1
    michail is offline Member
    Join Date
    Dec 2009
    Posts
    31
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You may wish to add a System.out.println() statement in the nested for loop, in the outer loop, just beneath the inner loop.

    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 
        }
    Do you understand why you need to do this?

  3. #3
    michail is offline Member
    Join Date
    Dec 2009
    Posts
    31
    Rep Power
    0

    Default

    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 that
    Last edited by michail; 12-23-2009 at 06:44 PM. Reason: Answered myself!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    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

  5. #5
    michail is offline Member
    Join Date
    Dec 2009
    Posts
    31
    Rep Power
    0

    Default

    That worked! I don't why the println helped!!
    Next step is to ask player to move!!

    Thanks.

    P.S. Any little ideas will help me allot along the way

Similar Threads

  1. a good java chess program
    By rico16135 in forum New To Java
    Replies: 10
    Last Post: 11-06-2010, 01:25 PM
  2. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  3. I want play chess in Java
    By ganzorig in forum Advanced Java
    Replies: 6
    Last Post: 11-15-2009, 05:53 AM
  4. New Chess-Like game
    By jSarK0Y in forum Reviews / Advertising
    Replies: 3
    Last Post: 06-10-2009, 04:28 AM
  5. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 07:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •