Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey Guys, I am making a tic tac toe game for school and I don't know why it is not running?

This is my Code:

Java
import java.util.Scanner;

public class TicTacToeOffical{

  public static Scanner in = new Scanner(System.in);

  public static int Introduction(){


    System.out.println("Hey");
    System.out.println("Today, you will play the game of Tic Tac Toe");
    System.out.println("So, let me go over few rules");
    System.out.println("Rule #1: This is how the Tic Tac Toe board will look like. ");

    try {
      Thread.sleep(1500);                          //Waits 1.5 Seconds
    }catch (Exception e) {
      System.out.println("Error");
    }


    System.out.println("           |          |          ");
    System.out.println("    0      |    1     |     2    ");
    System.out.println("           |          |          ");
    System.out.println("  -------------------------------");
    System.out.println("           |          |          ");
    System.out.println("    3      |    4     |     5    ");
    System.out.println("           |          |          ");
    System.out.println("  -------------------------------");
    System.out.println("           |          |          ");
    System.out.println("    6      |    7     |     8    ");
    System.out.println("           |          |          ");


    try {
      Thread.sleep(1500);
    }catch (Exception e) {                       //Waits 1.5 Seconds
      System.out.println("Error");
    }

    System.out.println("Rule #2: Choose a number to place your variable X or O");
    System.out.println("Lets say that you choose to be X and you choose the number 4");
    System.out.println("So now, the number 4 will be replacee with X");

    System.out.println("           |          |          ");
    System.out.println("    0      |    1     |     2    ");
    System.out.println("           |          |          ");
    System.out.println("  -------------------------------");
    System.out.println("           |          |          ");
    System.out.println("    3      |    X     |     5    ");
    System.out.println("           |          |          ");
    System.out.println("  -------------------------------");
    System.out.println("           |          |          ");
    System.out.println("    6      |    7     |     8    ");
    System.out.println("           |          |          ");

    try {
      Thread.sleep(2000);
    }catch (Exception e) {                           //Waits 1.5 Seconds
      System.out.println("Error");
    }

    System.out.println("Rule #3: To win the game, you have to get 3 X or O in a row ");

    System.out.println("           |          |          ");
    System.out.println("    0      |    X     |     2    ");
    System.out.println("           |          |          ");
    System.out.println("  -------------------------------");
    System.out.println("           |          |          ");
    System.out.println("    3      |    X     |     5    ");
    System.out.println("           |          |          ");
    System.out.println("  -------------------------------");
    System.out.println("           |          |          ");
    System.out.println("    6      |    X     |     8    ");
    System.out.println("           |          |          ");


    try {
      Thread.sleep(1500);
    }catch (Exception e) {                           //Waits 1.5 Seconds
      System.out.println("Error");
    }

    System.out.println("So, Would You Like to play Tic Tac Toe?");       // The user will have two options.
    System.out.println("Press 1 for yes and 2 for No");                 //  If the user wants to play, Press 1
    int answer = in.nextInt();                                         // If the user doesn't want to play then press 2
    if (answer == 1){
      return 2;
    }else if (answer == 2){
      return 3;
    }
    return 1;
  }


  private static int notPlaying(){

    System.out.println("So you decided not to play Tic Tac Toe");  // This is the part of the code, when the user
    System.out.println("******** THE END **********");            // doesn't want to play Tic Tac Toe.

    return -1;
  }




  private static int playTime(){

    System.out.println("So, Are you ready to play Tic Tac Toe?");
    System.out.println("Player #1 will be X");
    System.out.println("Player #2 will be O");
    System.out.println("Your game will begin in");

    try {
      Thread.sleep(1000);
    }catch (Exception e) {                       //Waits 1 Seconds
      System.out.println("Error");
    }

    System.out.println("3");

    try {
      Thread.sleep(1000);
    }catch (Exception e) {                       //Waits 1 Seconds
      System.out.println("Error");
    }

    System.out.println("2");
    try {
      Thread.sleep(1000);
    }catch (Exception e) {                       //Waits 1 Seconds
      System.out.println("Error");
    }
    System.out.println("1");

    try {
      Thread.sleep(1000);
    }catch (Exception e) {                       //Waits 1 Seconds
      System.out.println("Error");
    }


    String [] Array = new String [9];     // Created a new string array


    for (int i = 0; i < 9; i++){
      Array [i] = " ";
    }
    for (int i = 0; i < 9; i++){

      System.out.println("Player #1: Please enter a number between 0 to 8");
      int currentPlayer = in.nextInt();
      Array [currentPlayer] = "X";

      System.out.println("           |          |          ");
      System.out.println("       "+Array[0]+"   |     " +Array[1]+"    |     "+Array[2]+"     ");
      System.out.println("           |          |          ");
      System.out.println("  -------------------------------");
      System.out.println("           |          |          ");
      System.out.println("       "+Array[3]+"   |     " +Array[4]+"    |     "+Array[5]+"     ");
      System.out.println("           |          |          ");
      System.out.println("  -------------------------------");
      System.out.println("           |          |          ");
      System.out.println("       "+Array[6]+"   |     " +Array[7]+"    |     "+Array[8]+"     ");
      System.out.println("           |          |          ");

        if (Array [0] &&  Array [1] &&  Array [2] == Array[currentPlayer]){
        System.out.println("Player One Won");

        }
      System.out.println("Player #2: Please enter a number between 0 to 8");
      int otherPlayer = in.nextInt();
      Array [otherPlayer] = "O";

      System.out.println("           |          |          ");
      System.out.println("       "+Array[0]+"   |     " +Array[1]+"    |     "+Array[2]+"     ");
      System.out.println("           |          |          ");
      System.out.println("  -------------------------------");
      System.out.println("           |          |          ");
      System.out.println("       "+Array[3]+"   |     " +Array[4]+"    |     "+Array[5]+"     ");
      System.out.println("           |          |          ");
      System.out.println("  -------------------------------");
      System.out.println("           |          |          ");
      System.out.println("       "+Array[6]+"   |     " +Array[7]+"    |     "+Array[8]+"     ");
      System.out.println("           |          |          ");





    }
    return 1;
  }




  public static void main(String[] args){ // Main Method



    int finish = 1;

    while(finish != -1){

      if(finish == 1){
        finish = Introduction();

      }if (finish == 2){
        finish = playTime();
      }if (finish == 3){
        finish = notPlaying();
      }
    }
  }
}



The Problem: When I run this program, it says something like that.

1 error found:
File: C:\Users\Nilesh\Downloads\TicTacToeOffical.java  [line: 167]
Error: bad operand types for binary operator '&&'
  first type:  java.lang.String
  second type: java.lang.String


Can you please help me solve this problem.
Posted
Updated 16-May-15 13:16pm
v2
Comments
PIEBALDconsult 16-May-15 19:14pm    
It might be nice if you had posted only that line and the ones around it. Is it this one?
if (Array [0] && Array [1] && Array [2] == Array[currentPlayer]){
Jainam Patel 16-May-15 20:57pm    
yesss

Can you please help me with this
Jainam Patel 18-May-15 11:30am    
Hi, Can you please tell me what I have to do in order to check if the game is tie.


1 solution

The operator && (spoken: 'AndAlso') is used to logically combine its left expression with its right expression and requires both these expressions (Array[0] and Array[1] in your case) to evaluate to the type of boolean. But your array 'Array' is an array of strings and accessing an index (like Array[0]) evaluates to the type of string.

You did it right with the third expression: Array[2] == Array[currentPlayer] , it evaluates to boolean because you use == to compare two strings. You need to replace Array[0] and Array[1] also by expressions that evaluate to boolean. Probably like this:
Java
if (Array[0] == Array[currentPlayer] &&
    Array[1] == Array[currentPlayer] &&
    Array[2] == Array[currentPlayer])
{
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900