Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 24 of 24

Thread: Simple Chess program

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Exclamation Simple Chess program

    Hi, I'm trying to write a simple chess program for my java class. And I;m not exactly sure how to do it.
    We have these specifications:
    * Write a constructor with no arguments which initializes the instance of the new object with all
    squares empty.
    * Write a constructor with one argument String[] occupied, which is a list of those squares in the board that are occupied, using the same format as in Program 2.
    * The remove(int row, int col) method will remove the checker from the square (row,col).
    * The place(int row, int col) method will place a checker on the square (row,col).
    * The show(int row, int col) method will return a value indicating whether or not there is a game piece on the square (row,col).
    * The display() method will output a picture of the board.

    This is what I have, and I dont know what to do. I'm also taking a C++ class and overlapping programs dont help. As for the display method I dont have to display a GUI, just a board made up of dashes and lines using a bunch of println statements.
    class Board{
    Board(){
    int Board[][] = new int [8][8];
    for( int i = 0; i < 8; i++);
    for( int j; j < 8; j++);
    Board[i][j] = 0;
    }
    Board(String[] occupied){
    }
    void remove(int row, int col){

    }
    void place(int row, int col){

    }
    int show(int row, int col){

    }
    void display(){

    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,559
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: Simple Chess program

    Do you have some specific questions yet about the assignment?

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Simple Chess program

    Yes, I don't know how to implement anything. I just turned in a C++ assignment today and I don't know how to go about this program at all. I can make a board and thats about it :/

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,543
    Thanks
    24
    Thanked 298 Times in 281 Posts

    Default Re: Simple Chess program

    for( int i = 0; i < 8; i++);
    for( int j; j < 8; j++);

    Those are issues. See the post I provided here regarding this issue: Balloon.java, what in the world do i do? (second from last post).

    I was explaining constructors and methods, but I mentioned the same issue with loops. This is what you are doing, and as I state in the post from the link above, you will not get a compile or runtime error when you do this in loops.


    Now, to continue:
    1) It seems like a general question you may have is: how do I get an element from an array?
    Well, just like in C (I havent done C++ in detail, but C and C++ have similarities), you just do the following:
    int selection = Board[row][col];

    2) One of the most important things about what you are doing is to understand that ints will default to 0. Why is this important? Because when you do
    int Board[][] = new int [8][8];
    all of these indexes will have the value of 0 (not null like you would get with Objects). This is important for when you are indicating that a spot has a piece or not.

    3) Question: Why is Board[][] a 2D int array instead of a 2D boolean array, since you only seem to care if a spot has a piece on it or not?

    4) Remove Method -- It would seem that this method would want to set the indicated spot to the int default value of 0. So, just access that spot and set its value to 0:
    Board[row][col] = 0;

    5) Place Method -- It would seem that this method does the opposite of the Remove Method. Do the same thing as the Remove Method, but change the value the spot is changed to (like 1 instead of 0 or however you're doing it).

    6) Show Method -- For this method, access the position by using what I explained in #1, and just return the value of the cell, since that should indicate if the cell has a value in it or not.

    7) Display Method -- This is the fun method. Ok, there are two important things to do here: Include System.out.print statements and Include System.out.println statements. What is the difference?
    System.out.print(String) - Prints out the value, but keeps the cursor on the current line
    System.out.println(String) - Prints out the value and moves the cursor to the next line
    Since you have done programming before, tell me if this makes sense: System.out.println("Example") == System.out.print("Example\n")
    So, to do this with ease, we can go through each element in the array with nested loops. The inner loop contains System.out.print statements, while the outer loop contains the System.out.println statement. Here is an example (without the cell references, I'm sure you can figure that out):
    //Go through 10 Rows
    for(int x=0;x<10;x++)
    {
    	//Go through 10 Columns in each Row
    	for(int y=0;y<10;y++)
    	{
    		//Print out value, don't advance line, leave space after for splitting
    		System.out.print("V ");
    	}
    	//Advance to next line, you do not need to give this parameters, it will just move the cursor
    	System.out.println();
    }


    Tell me how much of that is useful.
    Last edited by aussiemcgr; September 17th, 2010 at 09:57 PM.

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Simple Chess program

    I think I'm understanding some of this. The ; at the end of my for loops was just out of habit I guess.
    1) Is that piece of code what I'm supposed to use if the space is occupied? Will I even need to use for loops
    if my arrays are already set at 0?
    2)When I create an array I thought it was automatically filled with random values?
    3) I'm not so sure why its an array of ints. I believe our professor told us to use ints so we could
    place a number on our rows and columns.
    4 & 5) Pretty straightforward
    6)Now when you say I can use what you provided for me in #1, do I include my for loops here too?
    7)I was unaware of the systemprint. statements. I only knew of the println. I understand the syntax you
    printed in bold and how they are equal, but I don't get what each is print statement is going to print out.
    I thank you very much!

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,543
    Thanks
    24
    Thanked 298 Times in 281 Posts

    Default Re: Simple Chess program

    I think I'm understanding some of this. The ; at the end of my for loops was just out of habit I guess.
    1) Is that piece of code what I'm supposed to use if the space is occupied? Will I even need to use for loops
    if my arrays are already set at 0?
    2)When I create an array I thought it was automatically filled with random values?
    3) I'm not so sure why its an array of ints. I believe our professor told us to use ints so we could
    place a number on our rows and columns.
    4 & 5) Pretty straightforward
    6)Now when you say I can use what you provided for me in #1, do I include my for loops here too?
    7)I was unaware of the systemprint. statements. I only knew of the println. I understand the syntax you
    printed in bold and how they are equal, but I don't get what each is print statement is going to print out.
    1) Ok, well when you create an array of ints using:
    int Board[][] = new int [8][8];
    You are saying you are using a two dimensional int array. A two dimensional array is an array that holds arrays. For your assignment, you are attempting to create a data structure that would act similar to a grid. A 2d array is what you want because it is how a grid would look like for humans, but for computers it is not quite as simple. So, the above code says we have an array of 8 things. Each of those things are arrays that hold 8 ints. So, to access a specific "cell" in the data structure, you would do this:
    //Where x is the X-Value you are getting in the grid, and y is the Y-Value you are getting in the grid
    int value = Board[x][y];
    Simple enough. But this is just a short-hand way of accessing that spot. The long way of doing it (which is how computers see it is this):
    //Where x is the X-Value you are getting in the grid, and y is the Y-Value you are getting in the grid
    int[] valueArray = Board[x];
    int value = valueArray[y];
    So, that is how the 2D Array actually works. When you attempt to reference a "cell" you are actually referencing a value inside an array inside the array. It is an odd complex to get your head around, but it might help you understand how it is actually accessing your data.

    So, when you use this code:
    //Where x is the X-Value you are getting in the grid, and y is the Y-Value you are getting in the grid
    int value = Board[x][y];
    It will give you the value assigned to the "cell" of that x and y coordinate. So, if you have not changed the value of that cell, it will be the int default value of 0. If you have changed its value, it will be whatever value you assign to it. See if you understand this better as I go through the rest of your questions.

    2) This is true of C and C++; well, sort of. In C and C++, the default value assigned to an int is its value in memory. However, since JAVA is not designed to have direct access to memory, simple types are given default values. ints are assigned a default value of 0.
    NOTE: Objects, however, have no default value. So if you create an Object and not assign it, you will get a compiler error if you attempt to use that Object later on in your code. The compiler error will say something like: "Variable may not have been initialized."
    To see a list of JAVA Default Values, have a look at this website:Java Quick Reference - Language Fundamentals - Default Values
    This means that when you create an array of simple types (like you have with your 2D array), all of those ints have automatically been set to the value of 0.

    3) My guess is that the professor will have you use 3 values that will be relevant for later. That or he hasnt wanted to go over booleans yet. Here are my guesses at his future values:
    1) Value 0 = Empty Cell
    2) Value 1 = Occupied by White Piece
    3) Value 2 = Occupied by Black Piece
    Just a guess for now. For you, I would work with an empty cell with the value of 0 and an occupied cell with the value of 1 (binary, yay). I would do it this way also because the binary values set to booleans (which you will probably learn later if not already) is 0 = FALSE and 1 = TRUE.

    6) #1 actually didnt include the loop stuff, that was more of a prelogue sort of thing. But rather, you want to look at referencing "cells" using this code:
    //Where x is the X-Value you are getting in the grid, and y is the Y-Value you are getting in the grid
    int value = Board[x][y];
    Then when you have the value, check if it is either occupied or empty. To determine this, use the understanding that an Empty Cell will give you a value of 0 and an Occupied Cell with a value of 1.

    7) My guess is that your professor wants a grid to be printed out. Using the code I provided for this in my earlier post, it will print out something like this:
    V V V V V V V V V V
    V V V V V V V V V V
    V V V V V V V V V V
    V V V V V V V V V V
    V V V V V V V V V V
    V V V V V V V V V V
    V V V V V V V V V V
    V V V V V V V V V V
    V V V V V V V V V V
    V V V V V V V V V V
    I would think that your professor wants you to print out the value of each cell (instead of the letter V like I did).


    I may have provided too much information. Tell me if there is anything that needs to be cleared up.

  7. #7
    Junior Member
    Join Date
    Sep 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Simple Chess program

    So for this occupied function
    Board(string[] occupied)
    my professor wants me to choose a couple values in my array and give them a value of 1, to show that tha space is occupied.How would I do that?
    For my previous chess program I did this

    board[2][1] = 1;
    board[1][4] = 1;

    when he returned my homework he put a bracket around this code and a question mark, So I can only presume I went about filling a couple values the wrong way. And for the same function when I try to compile it says
    '.class' expected
    and I don't know what it wants.

  8. #8
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,543
    Thanks
    24
    Thanked 298 Times in 281 Posts

    Default Re: Simple Chess program

    Ok, well I guess I have a question that is important. How is your professor wanting you to indicate a square as "occupied"?

    As for that function, or what appears to be an alternative constructor, it doesnt make much sense to me. I'm not sure what relevance the String[] is when you have a int[][] for the board.

    By saying: Board(String[] occupied), it would appears as if your professor wanted you to create a Board Object, that would hold the Board (int[][]), and all of it's information.

    Find some of this out and I can help you from there.

  9. #9
    Junior Member
    Join Date
    Sep 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Simple Chess program

    I myself am not absolutely positive at what he wants by this. I get a hold of him today, cause he wont be here until 2 and thats when my class is. And I just realized I have to also create a class TestDriver for this too. Is testing a class the same as in C++? I would still create an object of board and use the dot function to test my methods? I think this is what he wants for the (string[] args) function
    The main method should read its String[] args parameter, which should be a list of those squares in the board that are occupied. Each such square is indicated by a pair of integers in the range 0-7, separated by a comma. For example, if your program is called with
    java Board 4,3 7,0

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,559
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: Simple Chess program

    The main method for a java program has the arguments: main(String[] args)
    When you start the execution of the program, you put the arguments on the command line following the name of the class. These args are put into the String[] array that is passed to the main method.

    java YOURCLASSNAME arg0 arg1 ... argn

    In your example, arg0 = 4,3 and arg1 = 7,0
    java Board 4,3 7,0

  11. #11
    Junior Member
    Join Date
    Sep 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Simple Chess program

    So then do I fill the
    Board(String[] occupied){

    }

    with spaces that already contain pieces? Thats what this constructor is for, right?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,559
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: Simple Chess program

    What is the definition of the occupied array? What does it contain and what is it used for by the Board class constructor.

  13. #13
    Junior Member
    Join Date
    Sep 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Simple Chess program

    He wants us to initialize some spaces on the board to be occupied.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,559
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: Simple Chess program

    He wants us to initialize some spaces on the board to be occupied
    How do you locate a square and indicate that it is occupied?
    In a 2 dim board, a location would be row and column. Did "he" say you are to pass the args passed to the program to the Board constructor. The args passed to main where described above: java Board 4,3 7,0
    There are two Strings each with row,col format.

  15. #15
    Junior Member
    Join Date
    Sep 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Simple Chess program

    My compiler keeps on throwing me an error that it doesnt know what Board is when I try to initialize some spaces in the occupied string function

    Board(String[] occupied){
    Board[1][2] = 1;
    Board[5][1] = 1;
    Board[7][3] = 1;
    }

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,559
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: Simple Chess program

    My compiler keeps on throwing me an error
    Sorry, I can't see the text of the error message from here so I can't tell you what is wrong.

  17. #17
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,543
    Thanks
    24
    Thanked 298 Times in 281 Posts

    Default Re: Simple Chess program

    Ok, this statement:
    Board(String[] occupied){
    indicates you are declaring either a method or a constructor.
    QUESTION #1: Is this supposed to be a method or a constructor?

    If it is a Constructor:
    Then the name of your Object is a Board Object. In that case, you cant have a 2D array named Board[][] like you are saying you have when you code:
    Board[1][2] = 1;
    You should have a new Class (named Board), that has Board(String[] occupied) as its Constructor. Inside that class, you will have a 2D Array that will hold your spots of your Game Board Grid.

    If it is a Method:
    Then you need to specify what it returns. And you need to have a 2D Array named Board (which you obviously don't have based on the compiler error).


    QUESTION #2: What exactly is this String[] occupied? Each String must be formatted in some way so you can determine the spot that is occupied. That parameter needs to be explained in more detail.

  18. #18
    Junior Member
    Join Date
    Sep 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Simple Chess program

    public class Board{
    public Board(){
    int row, col;
    int Board[][] = new int [8][8];
    int value = Board[row][col];
    }
    public Board(String[] occupied){
    Board chessBoard = new Board();
    chessBoard[2][5] = occupied;
    }
    void remove(int row, int col){
    if(row > 8)
    System.exit(1);
    else if(col > 8)
    System.exit(1);
    else if(row < 0)
    System.exit(1);
    else if(col < 0)
    System.exit(1);
    Board[row][col] = 0;
    }
    void place(int row, int col){
    Board[row][col] = 1;
    }
    int show(int row, int col){
    int value = Board[x][y];
    if(value == 0){
    System.out.println("Your move has been made!");
    }else if(value = 1){
    System.out.println("This space is occupied");
    }
    }
    void display(){
    for(int x=0;Board.length;x++)
    {
    for(int y=0;Board.length;y++)
    {
    System.out.print("0");
    }
    System.out.println();
    }
    }
    }

    This is what I have, and in the occupied function it keeps saying I need an array.

  19. #19
    Junior Member
    Join Date
    Sep 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Simple Chess program

    Quote Originally Posted by aussiemcgr View Post
    Ok, this statement:
    Board(String[] occupied){
    indicates you are declaring either a method or a constructor.
    QUESTION #1: Is this supposed to be a method or a constructor?

    If it is a Constructor:
    Then the name of your Object is a Board Object. In that case, you cant have a 2D array named Board[][] like you are saying you have when you code:
    Board[1][2] = 1;
    You should have a new Class (named Board), that has Board(String[] occupied) as its Constructor. Inside that class, you will have a 2D Array that will hold your spots of your Game Board Grid.

    If it is a Method:
    Then you need to specify what it returns. And you need to have a 2D Array named Board (which you obviously don't have based on the compiler error).


    QUESTION #2: What exactly is this String[] occupied? Each String must be formatted in some way so you can determine the spot that is occupied. That parameter needs to be explained in more detail.
    This is what he has specified, he does want me to use the string as a constructor
    Write a constructor with one argument String[] occupied, which is a list of those squares in the board that are occupied.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,559
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: Simple Chess program

    A comment on your code - Don't use the same name for different objects:
    public class Board
    int Board[][] = new int [8][8];

    the occupied function
    Where is the occupied method?

    a list of those squares in the board that are occupied
    Look at the discussion above about the args being passed on the command line to the main method.
    It is a one dim array containing a "list of the occupied squares" in the format: row, col for each element.
    Last edited by Norm; September 20th, 2010 at 12:32 PM.

  21. #21
    Junior Member
    Join Date
    Sep 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Simple Chess program

    My bad, I meant method. I keep getting my C++ mixed with java.
    public Board(String[] occupied){
    Board chessBoard = new Board();
    chessBoard[2][5] = occupied;
    return chessBoard;
    }

  22. #22
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,543
    Thanks
    24
    Thanked 298 Times in 281 Posts

    Default Re: Simple Chess program

    Quote Originally Posted by x3rubiachica3x View Post
    My bad, I meant method. I keep getting my C++ mixed with java.
    public Board(String[] occupied){
    Board chessBoard = new Board();
    chessBoard[2][5] = occupied;
    return chessBoard;
    }
    A few notes:
    1) In a constructor, you do not create the Object (with the exception of very specific situations). So, this statement:
    Board chessBoard = new Board();
    will be in your Main, or Test Class.

    2) To my understanding, your board is a 2D array of ints. You cannot directly send a 2D array of ints a 1D array of Strings. That is where Norm and I are confused. Each String in the occupied array MUST be formatted in some way so you can separate the column and the row and locate that spot in your 2D int array. For example, Excel Cells are formatted like A3. A3 indicates Row 1 and Column 3. How are the occupied array Strings formatted? Give us an example of what it looks like.

    3) Constructors do not return anything. So this is statement will cause errors:
    return chessBoard;


    Here is an example of an Object Class, with its constructor, method, and variables:
     //Imports
    import java.awt.Point;
     
    public class Range
    {
    	//Object Variables
    	Point start;
    	Point end;
     
    	//Constructor
        public Range(Point s,Point e)
        {
        	//Object Variable Initalizations
        	start = s;
        	end = e;
        }
     
    	//Method that returns a String and overrides the Object's inherited toString() method
    	public String toString()
    	{
    		return "("+((int)start.getX())+","+((int)start.getY())+")-("+((int)end.getX())+","+((int)end.getY())+")";
    	}
    }
    This code is from a program I'm working on (also using a grid interestingly enough). This is not what your Board Object Class will look like. But, understanding what you are seeing above will go a long way to understanding how you make your own Object Classes.

  23. The Following User Says Thank You to aussiemcgr For This Useful Post:

    x3rubiachica3x (September 20th, 2010)

  24. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,559
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: Simple Chess program

    String[] occupied
    This defines an array of String. I think your instructor said that the args passed to the program into the main method's String[] args would be what you pass directly to the Board's constructor.
    The String[] args would be: {" 4,3", "7,0"} an array with two Strings.
    The Board constructor would have to parse/split each string in this array to get the x,y coordinates for the occupied squares: 4,3 and 7,0. These pairs of coordinates would be used with the chessBoard array to set the square as occupied:
    chessBoard[x][y] = ???
    where ??? is an indicator showing that the square is occupied.

  25. The Following User Says Thank You to Norm For This Useful Post:

    x3rubiachica3x (September 20th, 2010)

  26. #24
    Junior Member
    Join Date
    Sep 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Simple Chess program

    Thanks guys. I turned my homework in and hopefully I get a decent grade or at least some more feedback on what I did wrong

Similar Threads

  1. urgent simple help for a very simple program
    By albukhari87 in forum Java Applets
    Replies: 4
    Last Post: June 5th, 2010, 03:43 PM
  2. simple program
    By Memb in forum Paid Java Projects
    Replies: 0
    Last Post: March 17th, 2010, 01:47 PM
  3. Error of data types and type casting in java program
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 2nd, 2009, 10:22 AM
  4. PLEASE HELP!!!! simple java program...
    By parvez07 in forum Object Oriented Programming
    Replies: 5
    Last Post: August 26th, 2009, 06:38 AM
  5. Someone please help me write a simple program!!!
    By ocean123 in forum Loops & Control Statements
    Replies: 3
    Last Post: June 14th, 2009, 09:46 PM

Tags for this Thread