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
Give five functions of Windows​
Nata [24]

Answer:

Explanation:

It knows how to remember data and where to find that data. In other words, it knows how the raw memory works. (RAM).

It understands how to make a program run within the confines of its logical steps in an order that makes the program work the way the user intends.

It can be made to display short cuts, or any icon or picture (not an obvious talent).

It can search for unwanted intruders like ads or viruses.

It can can time events even to the point of getting your coffee ready for you with the proper add on.

6 0
3 years ago
Jordan has been asked to help his company find a way to allow all the workers to log on to computers securely but without using
lana66690 [7]

Answer:

Webcam

Explanation:

A webcam is mainly used for taking images or videos from the computer. the webcam is a combination of two basic words i.e. web and camera. webcam can be connected to any computer via USB. It can be mounted on the desktop and sometimes it comes inbuilt on the laptops.

A webcam is also used for face recognition function to login to a computer system.

so according to the scenario, the most appropriate answer is a webcam.

6 0
3 years ago
An effective team would never have ______.
inysia [295]
An ineffective leader
3 0
2 years ago
What allow you to write alphas on a computer keyboard
lions [1.4K]

Answer:

"To create any of these Greek letters using the Alt codes, simply press the "Alt" key while simultaneously typing the listed number. For example, to create the Greek letter Alpha (α), press the "Alt" key and type 224 using the keypad at the right side of your keyboard." - GOOGLE

5 0
2 years ago
Insert a row above the selected row
anastassius [24]
I cants see the "Selected Road" And I cant answer this unless you put that up. Sorry.
6 0
3 years ago
Read 2 more answers
Other questions:
  • Cyber security includes which of the following?
    7·1 answer
  • Which statement is true about the elements of the interface of a presentation program? The status bar appears at the top of the
    14·2 answers
  • By default, windows does not display ____________________ in windows explorer.
    8·1 answer
  • 3.24 LAB: Seasons
    13·1 answer
  • Define additional characteristics such as font weight or style for an html tag
    5·1 answer
  • Select two netiquette guidelines. In three to five sentences, explain why these guidelines make professional online communicatio
    15·2 answers
  • Before performing a Web Recorder task, which two options should the user ensure are setup correctly?
    6·2 answers
  • Worksheet Identify the devices for moving the cursor around your computer screen
    12·1 answer
  • HELP PLEASE
    7·1 answer
  • In dos operating system ,write a command to delete the directory as well as the files of the directory ''world'' on drive E.
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!