Results 1 to 2 of 2
  1. #1
    Antonio16 is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Logical error on a program using robots to create a board game

    Hello guys, so I have to create a board game for one of my computer science project using the becker. So for this game, each player has 4 pieces and they are allowed to move it once per turn. A user wins the game if he/she can line up 3 pieces diagonally/vertically/horizontally. I used a 2d array to keep track of the positions or spots that were taken by the robots. If it's taken by the user1, it will be set to 1, if it's taken by player2, then 2, if none of them, then 3.

    Here is my code. There's something wrong with it and I can't seem to figure out. Thanks in advance. Also if there's a way i can make it shorter, that would be good too!


    Java Code:
     import java.util.*;
    import becker.robots.*;
    import java.awt.*;
    
    public class BoardGame extends Object
    {
        public static void main(String[] args) 
        {
            BoardCity board = new BoardCity(6,6);
    
            BoardPieces [] pieces = new BoardPieces[8];
    
            pieces[0] = new BoardPieces(board, 1,1, Direction.EAST); 
            pieces[1] = new BoardPieces(board, 2,4, Direction.EAST);
            pieces[2] = new BoardPieces(board, 3,1, Direction.EAST);       
            pieces[3] = new BoardPieces(board, 4,4, Direction.EAST);
            pieces[4] = new BoardPieces(board, 1,4, Direction.EAST);
            pieces[5] = new BoardPieces(board, 2,1, Direction.EAST);  
            pieces[6] = new BoardPieces(board, 3,4, Direction.EAST);
            pieces[7] = new BoardPieces(board, 4,1, Direction.EAST);       
    
            //Read before checking below! I created a 2d array of int called gameboard which basically stores the position of the robots. It will be set to 1 if its the team 1 or 2 if its team 2 or 3, if its neither.
            //I used the get avenue and get street method to find the location of the robots and depending whihc robots move, it will change the array to 1 or 2 or 3.... you will figure out!
    
            int[][] gameboard = new int[6][6];
            boolean userWon;
            for (int i=0 ; i<4 ;i++ )
            {   
                int ave = pieces[i].getAvenue();
                int str = pieces[i].getStreet();
                gameboard[ave][str] = 1;
            }
    
            for (int i = 4; i<8;i++)
            {   
                int ave = pieces[i].getAvenue();
                int str = pieces[i].getStreet();
                gameboard[ave][str] = 2;
            }
    
              Scanner in = new Scanner(System.in);
            int robotChoose;
            String moveChoose;      
      
    do
            {
                System.out.println("Type.........to choose robot");   
                robotChoose = Integer.parseInt(in.nextLine());   
                if (robotChoose == 0 || robotChoose == 1 || robotChoose == 2 || robotChoose == 3 || robotChoose == 4 || robotChoose == 5 || robotChoose == 6 || robotChoose == 7)
                {                 
                    System.out.println("now type.... how would you like the robot to move");      
                    moveChoose = in.nextLine();           
    
                    int oldAve = pieces[robotChoose].getAvenue();
                    int oldStr = pieces[robotChoose].getAvenue();
                    gameboard[oldAve][oldStr] = 3;
                    if (moveChoose.equalsIgnoreCase("d"))// && pieces[robotChoose].frontIsClear())
                    {     
                        pieces[robotChoose].move(); 
                    }
                    else if (moveChoose.equalsIgnoreCase("a"))// && pieces[robotChoose].frontIsClear())
                    {
                        pieces[robotChoose].moveBack();             
                    }
                    else if (moveChoose.equalsIgnoreCase("w"))// && pieces[robotChoose].frontIsClear())
                    {
                        pieces[robotChoose].moveUp();            
                    }
                    else if (moveChoose.equalsIgnoreCase("s"))// && pieces[robotChoose].frontIsClear())
                    {
                        pieces[robotChoose].moveDown();
    
                    }
                    else 
                    {
                        System.out.println("Please input only A, S, D, or W");
                        moveChoose = in.nextLine();  
                    } 
                }
                else 
                {
                    System.out.println("Please input number from 0 to 7");
                    robotChoose = Integer.parseInt(in.nextLine());
                }
    
                if (robotChoose == 0 || robotChoose == 1 || robotChoose == 2 || robotChoose == 3)
                {
                    int newAve = pieces[robotChoose].getAvenue();
                    int newStr = pieces[robotChoose].getAvenue();
                    gameboard[newAve][newStr] = 1;
                }
                else if (robotChoose == 4 || robotChoose == 5 || robotChoose == 6 || robotChoose == 7)
                {
                    int newAve = pieces[robotChoose].getAvenue();
                    int newStr = pieces[robotChoose].getAvenue();
                    gameboard[newAve][newStr] = 2;
                }          
    
                for (int i = 1; i<5; i++)
                {
                    for (int j = 1; j<3; j++)
                    {    
                        if (gameboard[i][j] == 1 && gameboard[i][j+1] == 1 && gameboard[i][j+2] == 1)
                        {
                            System.out.println("Yellow Win");
                            userWon = true;
                        }
                        else if (gameboard[i][j] == 2 && gameboard[i][j+1] == 2 && gameboard[i][j+2] == 2)
                        {
                            System.out.println("Purple Win");
                            userWon = true;
                        }
                    }
                }
    
                for (int i = 1; i<5; i++)
                {
                    for (int j = 1; j<3; j++)
                    {    
                        if (gameboard[j][i] == 1 && gameboard[j][i+1] == 1 && gameboard[j][i+2] == 1)
                        {
                            System.out.println("Yellow Win");
                            userWon = true;
                        }
                        else if (gameboard[j][i] == 1 && gameboard[j][i+1] == 1 && gameboard[j][i+2] == 1)
                        {
                            System.out.println("Purple Win");
                            userWon = true;
                        }
                    }
                }
    
                for(int i = 1; i<3; i++)
                {
                    if (gameboard[i][i] == 1 && gameboard[i+1][i+1] == 1 && gameboard[i+2][i+2] == 1)
                    {
                        System.out.println("Yellow Win");
                        userWon = true;
                    }
                    else if (gameboard[i][i] == 2 && gameboard[i+1][i+1] == 2 && gameboard[i+2][i+2] == 2)
                    {
                        System.out.println("Purple Win");
                        userWon = true;
                    }
                }
    
                if ((gameboard[1][2] == 1 && gameboard[2][3] == 1 && gameboard[3][4] == 1) || (gameboard[2][1] == 1 && gameboard[3][2] == 1 && gameboard[4][3] == 1))
                {
                    System.out.println("Yellow Win");
                    userWon = true;
                }
                else if ((gameboard[1][2] == 2 && gameboard[2][3] == 2 && gameboard[3][4] == 2) || (gameboard[2][1] == 2 && gameboard[3][2] == 2 && gameboard[4][3] == 2))
                {
                    System.out.println("Purple Win");
                    userWon = true;
                }
            } while (userWon = false);
        } 
    }
    Last edited by Antonio16; 01-13-2013 at 04:04 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Logical error on a program using robots to create a board game

    You didn't have the courtesy to reply to the two responses to your earlier question. Why should we expect any better behavior this time round?

    Also, any answers here are more than likely to be in the same vein. We can't advise on the use of classes that we know nothing about.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Java board game
    By sumys in forum New To Java
    Replies: 10
    Last Post: 03-18-2012, 09:23 PM
  2. How do i create this board ?
    By hunterr1234 in forum AWT / Swing
    Replies: 21
    Last Post: 10-30-2011, 08:19 PM
  3. Replies: 3
    Last Post: 09-26-2010, 06:29 AM
  4. game board design
    By tomitu in forum New To Java
    Replies: 12
    Last Post: 02-27-2010, 05:31 PM
  5. how to create board in java...
    By zenith in forum New To Java
    Replies: 5
    Last Post: 01-20-2010, 04:42 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
  •