Please help me..

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Please help me..

Post by philG17 on Wed Aug 28, 2013 9:26 am
([msg=77083]see Please help me..[/msg])

Hi guys. I don't know what will I do after this code. I am creating a game called SOS. I don't know if it's a Philippine game because I surfed the internet and no results were found. Anyway, it is a 3x3 board game and it has the same concept as Tic-Tac-Toe but in this game, the players cannot choose whether they will play as X or O, the only rule in this game is to form "SOS". Our prof added, our program should be terminated after all the positions are filled, and each formed "SOS" will be added to the player who formed "SOS". My problem is about scoring. After entering SOS in the first row which is (- - -), I tried to input O in the second row first column, and the player 2 will increment. It should not increment because it didn't satisfy the second else if in my program.. why is it like that?
Code: Select all
import java.util.Scanner;

public class SOS
{
  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);
    String ar[][] = {{"-","-","-"}, {"-","-","-"}, {"-","-","-"}};
    int player1 = 0;
    int player2 = 0;
    int i;
   
    for(int l = 0; l<3; l++)
    {
      for(int j = 0; j<3; j++)
      {
        System.out.print(ar[l][j]);
      }
      System.out.print("\n");
    }
   
    for(i = 0;i<9;i++)
    {
      int row = 0;
      int col = 0;
           
      System.out.println("Enter the row number: ");
      row = input.nextInt();
      System.out.println("Enter the column number: ");
      col = input.nextInt();
      input.nextLine();
      System.out.println("Enter (S or O): ");
      ar[row][col] = input.nextLine();
     
      for(int k = 0; k<3; k++)
      {
        for(int j = 0; j<3; j++)
        {
          System.out.print(ar[k][j]);
        }
        System.out.print("\n");
      }
if(ar[0][0].equalsIgnoreCase(ar[0][2]) && ar[0][1].equalsIgnoreCase("O") && !(ar[0][1].equalsIgnoreCase("S")) && !!(ar[0][0].equalsIgnoreCase("")))
      {
        if(i % 2 == 0)
        {
          player1++;
          System.out.println("Player 1: "+player1+"    Player 2: "+player2);
        }
        else
        {
          player2++;
          System.out.println("Player 1: "+player1+"    Player 2: "+player2);
        }
      }
      else if(ar[1][0].equalsIgnoreCase(ar[1][2]) && ar[1][1].equalsIgnoreCase("O") && !(ar[1][1].equalsIgnoreCase("S")) && !(ar[1][0].equalsIgnoreCase("")))
      {
         if(i % 2 == 0)
        {
          player1++;
          System.out.println("Player 1: "+player1+"    Player 2: "+player2);
        }
        else
        {
          player2++;
          System.out.println("Player 1: "+player1+"    Player 2: "+player2);
        }
      }
      else
        System.out.println("Player 1: "+player1+"    Player 2: "+player2);

}
}
}



Please help me.. My bestfriend needs it tomorrow. That's my only problem, the scoring...
philG17
New User
New User
 
Posts: 5
Joined: Tue Aug 20, 2013 11:40 pm
Blog: View Blog (0)


Re: Please help me..

Post by ghost107 on Wed Aug 28, 2013 12:50 pm
([msg=77087]see Re: Please help me..[/msg])

For future references please specify the language:
Few thing to start of:

In the example below sais,if( ar[0][0] == a[0][2] && ar[0][1] ==""O" && ar[0][1] != "S" && ??
Code: Select all
if(ar[0][0].equalsIgnoreCase(ar[0][2]) && ar[0][1].equalsIgnoreCase("O") && !(ar[0][1].equalsIgnoreCase("S")) && !!(ar[0][0].equalsIgnoreCase("")))

But if you set the ar[0][1] to be equal to "O" you don't need to check it if its not equal:
ar[0][1].equalsIgnoreCase("O") && !(ar[0][1].equalsIgnoreCase("S"))

The Operator !! I never saw it in in Java
!!(ar[0][0].equalsIgnoreCase("")

Same goes for the Else.

So now on your problem:
The 2 ifs will check The first and second Row where is ar[0][0], ar[0][2] and ar[0][1], it will not check the full matrix, or the current row, so if the first row is completed it will be stuck there.
So you will need to make a algorithm to check, and flag all the rows, if found or not found.

The simple way to fix it is to flag the rows as completed:
Code: Select all
Boolean bCompletedRows[2] = {true,true};
...
if(...&& bCompletedRows[0] ){
   ....
   bCompleted[0] = false;
}else if(...&&bCompletedRows[1] ){
   ....
  bCompleted[1] = false;
}else{

}
ghost107
Poster
Poster
 
Posts: 132
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests