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

• 01-13-2013, 05:01 AM
Antonio16
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!

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);     } }```
• 01-13-2013, 05:55 AM
DarrylBurke
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