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
What new information, strategies, or techniques have you learned that will increase your technology skills? Explain why its impo
Minchanka [31]

Taking brakes every hour so your eyes don't start to strain.

6 0
3 years ago
Which is an internet service?<br><br> Antivirus<br> Chat<br> Firewall<br> Router
leva [86]

Answer:

Router

Explanation:

The others are not related to internet per se.

8 0
3 years ago
Read 2 more answers
WILL GIVE BRAINLIEST!!!!!!!
pishuonlain [190]

Answer:

False

Explanation:

Central Louisiana Regional Port is the small river port (not in a top 100 ports), while New Orleans Port is  $100 million a year reveniew huge port, 7th busiest in US.

3 0
2 years ago
One friend claims that a dual-core system runs at twice the speed as a single-core system; another friend disagrees by saying th
lidiya [134]

Answer:

<u>the first friend</u>

Explanation:

It is important to <em>remember </em>that a core is the brain of the CPU (central processing unit), which means one who has a dual-core is having <em>"a dual brain" </em><em>to process information faster.</em>

Consider also, IT experts often acknowledge that in terms of speed of execution, it is proven that, "dual-core systems" are <em>faster</em> (even twice faster) than a "single-core system". The other friends were wrong because they disagreed with a widely accepted fact that dual-core is faster than single-core; and of course, <em>we know that without them being faster they can't run twice the applications and twice the data. </em>

Therefore, we can make the conclusion that the first friend's response is correct.

5 0
2 years ago
I have a question, but it's not a math question. Can anyone give me 5 unblockers for school computers? If you answer this, you w
ValentinkaMS [17]

Answer:

I mean if your talking about games then just try Cool math I will give link in comments

Explanation:

4 0
1 year ago
Read 2 more answers
Other questions:
  • If you are planning to carry a large balance on your credit card, which of the following credit card features should you look fo
    7·2 answers
  • Which of the following is an example of a logic bug?
    8·1 answer
  • ​to prevent intrusions, companies install combinations of hardware and software called _____ to keep unauthorized web users from
    9·1 answer
  • End-user development: the process by which an organization's non-it specialists create software applications.
    8·1 answer
  • Define a function CoordTransform() that transforms its first two input parameters xVal and yVal into two output parameters xValN
    7·2 answers
  • What is used to monitor the activity of a network and notify network administrators when it identifies something as suspicious?
    15·1 answer
  • To defragment a disk means to ____. slow it down, diagnose problems with it, reorganize it, repair it
    8·1 answer
  • Do you have to make a account of Windows 10?
    12·1 answer
  • Each student has a record on a file consisting of the following data: Student last name, Student ID (numeric), GPA (a decimal nu
    7·1 answer
  • When you type in text in an image in a photo-editing software, where is it created?
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!