14 Replies - 1406 Views - Last Post: 29 August 2013 - 05:04 AM Rate Topic: -----

#1 DaMi25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 09-December 12

SOS Game

Posted 28 August 2013 - 03:27 AM

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. Here is my code:
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("S") && ar[0][1].equalsIgnoreCase("O") && ar[0][2].equalsIgnoreCase("S"))
        {
          if(i % 2 == 0)
          {
            player1++;
            System.out.println("Player 1: "+player1+"     Player 2: "+player2);
          }
          else
          {
            player2++;
            System.out.println("Player 1: "+player2+"     Player 2: "+player2);
          }
        }
        else if(ar[1][0].equalsIgnoreCase("S") && ar[1][1].equalsIgnoreCase("O") && ar[1][2].equalsIgnoreCase("S")) 
        {
          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[2][0].equalsIgnoreCase("S") && ar[2][1].equalsIgnoreCase("O") && ar[2][2].equalsIgnoreCase("S")) 
        {
          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);
        }
   }
}
}


This post has been edited by DaMi25: 28 August 2013 - 04:30 AM


Is This A Good Question/Topic? 0
  • +

Replies To: SOS Game

#2 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: SOS Game

Posted 28 August 2013 - 03:40 AM

Quote

My problem is about scoring

That is not helpful at all. We also like to see an attempt first. I don't see anything related to scoring in your code. You may want to look at tic tac toe examples to see how it may be done.
Was This Post Helpful? 0
  • +
  • -

#3 megabit  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 79
  • Joined: 03-May 13

Re: SOS Game

Posted 28 August 2013 - 03:52 AM

View PostDaMi25, on 28 August 2013 - 03:27 AM, said:

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. Here is my code:


So the winner is the player who adds a letter and in doing so creates the combination SOS? I'm assuming that its like Tic-Tac-Toe where any combination of Rows, Columns or Diagonals win?

There are two ways you can do scoring.

1. (With strings)
a) Each time after a player moves
b] Check all rows, columns and diagonals for the combination SOS
c) If such combination exists, the player that last moved is the winner.

2. (With numbers)(assign 1 to 'S' and 10 to 'O')
a) Each time after a player moves
b] Sum all the rows, columns and diagonals
c) If a total of 12 (1+10+1 = S+O+S) exists, the player that last moved is the winner.

The 2nd option you will have to use a hidden array of ints for the number logic

This post has been edited by megabit: 28 August 2013 - 03:55 AM

Was This Post Helpful? 3
  • +
  • -

#4 DaMi25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 09-December 12

Re: SOS Game

Posted 28 August 2013 - 03:54 AM

View PostMylo, on 28 August 2013 - 10:40 AM, said:

Quote

My problem is about scoring

That is not helpful at all. We also like to see an attempt first. I don't see anything related to scoring in your code. You may want to look at tic tac toe examples to see how it may be done.


I'm sorry. I will code it right now.
Was This Post Helpful? 0
  • +
  • -

#5 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: SOS Game

Posted 28 August 2013 - 03:58 AM

I didn't mean to say that you must code it, perhaps that's my fault in communication. But you should address what you need more specifically. For example, if you wanted to know ways that you could do the scoring, we'd help out, that's fine. But 'I have a problem.' doesn't indicate what you are having a problem with.
Was This Post Helpful? 0
  • +
  • -

#6 DaMi25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 09-December 12

Re: SOS Game

Posted 28 August 2013 - 04:23 AM

View PostMylo, on 28 August 2013 - 10:58 AM, said:

I didn't mean to say that you must code it, perhaps that's my fault in communication. But you should address what you need more specifically. For example, if you wanted to know ways that you could do the scoring, we'd help out, that's fine. But 'I have a problem.' doesn't indicate what you are having a problem with.


I see.. Anyway, here is my code. I coded this in school but it showed bugs that's why I erased it and think of a new one. But until now, I can't think of any way.. megabit's suggestion are good but I don't know how to start.
Was This Post Helpful? 0
  • +
  • -

#7 megabit  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 79
  • Joined: 03-May 13

Re: SOS Game

Posted 28 August 2013 - 04:37 AM

View PostDaMi25, on 28 August 2013 - 04:23 AM, said:

I see.. Anyway, here is my code. I coded this in school but it showed bugs that's why I erased it and think of a new one. But until now, I can't think of any way.. megabit's suggestion are good but I don't know how to start.


So, every time a player gets to have a turn, they choose to use S or O? You need to define what the requirements of your program will be, to begin. You will probably need some methods like these:

checkIfEmpty() //this method will check if the position entered by the user is empty, can't put an 'S' or 'O' if it is already occupied!
setMove() //this method will set 'S' or 'O' to a postion on the board depending on where the user specifies.
checkIfWin() //this method will check if the last move was a winning move, if the game is a draw, or if the game will continue on.

You should probably also verify the input from the player is valid, or you will get ArrayOutOfBounds errors when trying to place it into your array.

EDIT: ok, I see you added to you original code at the top of the page. Try breaking your program up into methods, this will make it more manageable and more readable, Also post modifications separatly to your original code.

This post has been edited by megabit: 28 August 2013 - 04:41 AM

Was This Post Helpful? 1
  • +
  • -

#8 DaMi25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 09-December 12

Re: SOS Game

Posted 28 August 2013 - 05:11 AM

View Postmegabit, on 28 August 2013 - 11:37 AM, said:

View PostDaMi25, on 28 August 2013 - 04:23 AM, said:

I see.. Anyway, here is my code. I coded this in school but it showed bugs that's why I erased it and think of a new one. But until now, I can't think of any way.. megabit's suggestion are good but I don't know how to start.


So, every time a player gets to have a turn, they choose to use S or O? You need to define what the requirements of your program will be, to begin. You will probably need some methods like these:

checkIfEmpty() //this method will check if the position entered by the user is empty, can't put an 'S' or 'O' if it is already occupied!
setMove() //this method will set 'S' or 'O' to a postion on the board depending on where the user specifies.
checkIfWin() //this method will check if the last move was a winning move, if the game is a draw, or if the game will continue on.

You should probably also verify the input from the player is valid, or you will get ArrayOutOfBounds errors when trying to place it into your array.

EDIT: ok, I see you added to you original code at the top of the page. Try breaking your program up into methods, this will make it more manageable and more readable, Also post modifications separatly to your original code.


Thanks for the tips. I'm planning on making the oop approach after I figure out the condition in determining the winning combination. I'll show the revised code. Wait.
Was This Post Helpful? 0
  • +
  • -

#9 DaMi25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 09-December 12

Re: SOS Game

Posted 28 August 2013 - 05:17 AM

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);



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 satify the second else if in my program.. why is it like that?
Was This Post Helpful? 0
  • +
  • -

#10 megabit  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 79
  • Joined: 03-May 13

Re: SOS Game

Posted 28 August 2013 - 10:32 AM

View PostDaMi25, on 28 August 2013 - 05:17 AM, said:

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);



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 satify the second else if in my program.. why is it like that?


Ok, so put all this code into a method for determining if there is a winner. Like this.

//this method will return true if there is a winner
public boolean checkIfWinner(String theArray)
{

    //if, the characters in a ROW(there are 3 rows remember) equal 'S' 'O' 'S'
    //return true;

    //else if, the characters in a COLUMN(again 3) equal 'S' 'O' 'S'
    //return true;

    //else if, the characters in a DIAGONAL(only 2 of them) equal 'S' 'O' 'S'
    //return true;

    //else, there is no winner yet
    //return false;

}


This post has been edited by megabit: 28 August 2013 - 10:36 AM

Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: SOS Game

Posted 28 August 2013 - 10:48 AM

There are more intelligents ways of achieving that type of thing with 1/10 of the number of code
assihgn prime numbers values to S and O based if they are in row 0,1,2 or col 0,1,2 and diagonal 0,1,2

The multiplication of the 3 elements (row, column, diagonal) will be unique and always the same for the word SOS
Was This Post Helpful? 1
  • +
  • -

#12 megabit  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 79
  • Joined: 03-May 13

Re: SOS Game

Posted 28 August 2013 - 11:27 AM

View Postpbl, on 28 August 2013 - 10:48 AM, said:

There are more intelligents ways of achieving that type of thing with 1/10 of the number of code
assihgn prime numbers values to S and O based if they are in row 0,1,2 or col 0,1,2 and diagonal 0,1,2

The multiplication of the 3 elements (row, column, diagonal) will be unique and always the same for the word SOS

Yep, I already suggested using the number technique. It's far simpler, less to write, and the visible board can still be displayed over the top of it so to speak.
Was This Post Helpful? 0
  • +
  • -

#13 DaMi25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 09-December 12

Re: SOS Game

Posted 28 August 2013 - 05:24 PM

How will I assign a prime number for each position?
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: SOS Game

Posted 28 August 2013 - 06:06 PM

This is an example on how to do it with all the possible combination of S and O

		// a  for each row/column/diagonal
		int[] rowColValue = {3, 5, 7};
		// a prime for S and one for O
		int sValue = 11;
		int oValue = 13;
		
		// possible combinations
		String[] combinArray = {"OOO", "OOS", "OSO", "OSS", "SOO", "SOS", "SSO", "SSS"};
		
		int posValue;
		for(String combin : combinArray) {
			int value = 0;
			System.out.print(combin + " yields: ");
			for(int i = 0; i < 3; ++i) {
				char letter = combin.charAt(i);
				
				// evaluate value of letter at that position
				if(letter == 'S')
					posValue = sValue;
				else
					posValue = oValue;
				if(i > 0)
					 System.out.print(" + ");
				System.out.print("(" + posValue + " * " + rowColValue[i] + ")");
				value += (posValue * rowColValue[i]);
			}
			System.out.println(" = " + value);
		}
	    


which prints
OOO yields: (13 * 3) + (13 * 5) + (13 * 7) = 195
OOS yields: (13 * 3) + (13 * 5) + (11 * 7) = 181
OSO yields: (13 * 3) + (11 * 5) + (13 * 7) = 185
OSS yields: (13 * 3) + (11 * 5) + (11 * 7) = 171
SOO yields: (11 * 3) + (13 * 5) + (13 * 7) = 189
SOS yields: (11 * 3) + (13 * 5) + (11 * 7) = 175 <--- here is 175 the unique value for SOS
SSO yields: (11 * 3) + (11 * 5) + (13 * 7) = 179
SSS yields: (11 * 3) + (11 * 5) + (11 * 7) = 165


175 if S are in first and third position and O in the middle
Was This Post Helpful? 1
  • +
  • -

#15 DaMi25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 09-December 12

Re: SOS Game

Posted 29 August 2013 - 05:04 AM

View Postpbl, on 29 August 2013 - 01:06 AM, said:

This is an example on how to do it with all the possible combination of S and O

		// a  for each row/column/diagonal
		int[] rowColValue = {3, 5, 7};
		// a prime for S and one for O
		int sValue = 11;
		int oValue = 13;
		
		// possible combinations
		String[] combinArray = {"OOO", "OOS", "OSO", "OSS", "SOO", "SOS", "SSO", "SSS"};
		
		int posValue;
		for(String combin : combinArray) {
			int value = 0;
			System.out.print(combin + " yields: ");
			for(int i = 0; i < 3; ++i) {
				char letter = combin.charAt(i);
				
				// evaluate value of letter at that position
				if(letter == 'S')
					posValue = sValue;
				else
					posValue = oValue;
				if(i > 0)
					 System.out.print(" + ");
				System.out.print("(" + posValue + " * " + rowColValue[i] + ")");
				value += (posValue * rowColValue[i]);
			}
			System.out.println(" = " + value);
		}
	    


which prints
OOO yields: (13 * 3) + (13 * 5) + (13 * 7) = 195
OOS yields: (13 * 3) + (13 * 5) + (11 * 7) = 181
OSO yields: (13 * 3) + (11 * 5) + (13 * 7) = 185
OSS yields: (13 * 3) + (11 * 5) + (11 * 7) = 171
SOO yields: (11 * 3) + (13 * 5) + (13 * 7) = 189
SOS yields: (11 * 3) + (13 * 5) + (11 * 7) = 175 <--- here is 175 the unique value for SOS
SSO yields: (11 * 3) + (11 * 5) + (13 * 7) = 179
SSS yields: (11 * 3) + (11 * 5) + (11 * 7) = 165


175 if S are in first and third position and O in the middle


Thank you. But I didn't get this. I don't know how to do this but anyway, I finished the program. What I did was not in a object-oriented manner. I will post as soon as I finished revising it in OOP style. Thanks all for your help. Thank you! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1