1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Amiraneli [1.4K]
3 years ago
15

Write a program for playing tic tac toe. Two players take turns marking an available cell in a 3 X 3 grid with their respective

tokens (either X or O). When one player has placed the three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. When a token is entered, the program redisplays the board on the console and determines the status of the game. (Win or Draw).
Computers and Technology
1 answer:
Fynjy0 [20]3 years ago
3 0

Answer:

import java.util.*;

public class tictactoe{

  //array to store values inserted in the bord

  static char[][] board = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};

  //method to display board

  public static void display(){

      System.out.println("-------------");

      for(int i=0;i<3;i++){

          for(int j=0;j<3;j++){

              System.out.print("|");

              if(board[i][j] != ' ')

                  System.out.print(board[i][j]);

          }

          System.out.println("|");

      }

      System.out.println("-------------");

  }

  //method for player1 to make choice

  public static void player1(){

      Scanner in = new Scanner(System.in);

      System.out.print("Enter a row for player X: ");

      int row = in.nextInt();

      System.out.print("Enter a column for player X: ");

      int col = in.nextInt();

      //if the value are not valid and not between 0-2

      if(row > 2 || row < 0 || col > 2 || col < 0){

          System.out.println("Invalid Choice");

          player1();

      }

      //if selected position is empty

      else if(board[row][col] == ' '){

          board[row][col] = 'X';

          check('X',row,col);

          player2();

      }

      //if selected position is not empty

      else{

          System.out.println("Already filed, Enter another choice");

          player1();

      }

  }

  //method for player2 to make choice

  public static void player2(){

      Scanner in = new Scanner(System.in);

      System.out.print("Enter a row for player O: ");

      int row = in.nextInt();

      System.out.print("Enter a column for player O: ");

      int col = in.nextInt();

      //if the value are not valid and not between 0-2

      if(row > 2 || row < 0 || col > 2 || col < 0){

          System.out.println("Invalid Choice");

          player2();

      }

      //if selected position is empty

      else if(board[row][col] == ' '){

          board[row][col] = 'O';

          check(')',row,col);

          player1();

      }

      //if selected position is not empty

      else{

          System.out.println("Already filed, Enter another choice");

          player2();

      }

  }

  //method to check whether a player has won

  public static void check(char s, int row, int col){

      display();

      //check row has same 3 values

      int count = 0;

      for(int i=0;i<3;i++){

          if(board[row][i] == s)

              count++;

      }

      if(count == 3){

          System.out.println(s + " player won");

          System.exit(0);

      }

      //checking column has same 3 values

      count = 0;

      for(int i=0;i<3;i++){

          if(board[i][col] == s)

              count++;

      }

      if(count == 3){

          System.out.println(s + " player won");

          System.exit(0);

      }

      //checking first diagonal has same 3 values

      count = 0;

      for(int i=0;i<3;i++){

          if(board[i][i] == s)

              count++;

      }

      if(count == 3){

          System.out.println(s + " player won");

          System.exit(0);

      }

      //checking second diagonal has same 3 values

      count = 0;

      for(int i=0;i<3;i++){

          if(board[i][2-i] == s)

              count++;

      }

      if(count == 3){

          System.out.println(s + " player won");

          System.exit(0);

      }

      //checking whether all places are filled

      count = 0;

      for(int i=0;i<3;i++)

          for(int j=0;j<3;j++)

              if(board[i][j] == ' ')

                  count++;

      if(count == 0){

          System.out.println("Game tied");

          System.exit(0);

      }

  }

  public static void main(String[] args){

      display();

      player1();

  }

}

Explanation:

You might be interested in
Write the pseudocode for a program that will process attendance records of CA students. The students attend college five days a
Alenkasestr [34]

Using the knowledge of pseudocodes it will be possible to write a code that calculates the amount of hours worked and giving warnings about it.

<h3>Writing a pseudocode we have that:</h3>

<em>while </em>

<em>if number == 0</em>

<em>break</em>

<em>hours =0 </em>

<em>for i =1 to 5 </em>

<em>hours = hours + time_out[ i ] - time_in[ i ]</em>

<em> If hours >36 : </em>

<em>print ( "Name is " ,name)</em>

<em>print( "No of hours are ",hours)</em>

<em>print("Congratulaion! Your working hours are more than 36")</em>

<em>If hours <30 : #</em>

<em>print ( "Name is " ,name)</em>

<em>print( "No of hours are ",hours)</em>

<em>print("Warning !!!")</em>

<em>End loop</em>

See more about pseudocode at brainly.com/question/13208346

#SPJ1

3 0
2 years ago
Which of the following is an example of data an Earth-observing satellite would collect?
Natalka [10]

Answer:

A

Explanation:

Hopefully this helps

4 0
2 years ago
Speed is how fast an object moves a certain distance within a length of time. How is speed calculated?
Butoxors [25]

I believe the answer to be A) because distance with the time it took to to reach the distance would determine how speed is to be calculated.

4 0
3 years ago
Read 2 more answers
For each problem listed below, use the drop-down menu to select the field of the professional who can help solve the issue.
Lapatulllka [165]

Answer:

An employee is having trouble opening a file on a computer.

  • → ✔ <u>information services and support</u>

The president of a company wants to give the company website a fresh new look.

  • → ✔ <u>interactive media</u>

An employee wants to work from home but can’t connect to the network from there.

  • → ✔ <u>network systems administration</u>

The vice president of sales would like help designing a new software program to keep track of sales.

  • → ✔<u> programming and software development</u>

<u>OAmalOHopeO</u>

5 0
2 years ago
Read 2 more answers
Write a program to prepare the monthly charge account statement for a customer of CS CARD International, a credit card company.
WITCHER [35]

Answer:

import java.util.*;

import java.text.*;

class CreditCardBill

{

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

NumberFormat defaultFormat = NumberFormat.getCurrencyInstance(Locale.US);

System.out.println("CS Card International Statement");

System.out.println("===============================");

System.out.print("Previous Balance: $");

double prevBalance = sc.nextDouble();

System.out.print("Additional Charges: $");

double addCharges = sc.nextDouble();

double interest;

if(prevBalance == 0)

interest = 0;

else

interest = (prevBalance + addCharges) * 0.02;

System.out.println("Interest: "+defaultFormat.format(interest));

double newBalance = prevBalance + addCharges + interest;

System.out.println("New Balance: "+defaultFormat.format(newBalance));

double minPayment;

if(newBalance < 50)

minPayment = newBalance;

else if(newBalance <= 300)

minPayment = 50.00;

else

minPayment = newBalance * 0.2;

System.out.println("Minimum Payment: "+defaultFormat.format(minPayment));

}

}

5 0
3 years ago
Other questions:
  • Although the original BBS system was simple, it increased people's ability to communicate
    14·1 answer
  • New Jersey and New York have the highest state taxes in the United States. They also have high ratios of people moving out compa
    13·1 answer
  • How many check boxes from the following code can be selected at any given time? Home Address Federal Express UPS
    13·1 answer
  • Which of the following statements is false? a. InputStream and OutputStream are abstract classes for performing byte-based I/O.
    7·1 answer
  • How do you merge on excel?​
    5·1 answer
  • Give one example of where augmented reality is used​
    11·2 answers
  • A soft news story and a feature story are the same thing.<br> O True<br> O False<br> HELLPP ASSAAAP
    12·2 answers
  • I WILL GIVE BRAINLIST THING TO WHOEVER GIVES ME THE CORRECT ANSWER
    11·1 answer
  • DIRECTIONS: Organize your desktop. Name the 5 folders based on the files given below. Organize your own desktop by sorting the g
    12·1 answer
  • Write a Pascal program that will prompt the user to enter the radius of a circle.
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!