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
Zbn-ivfh-ovm is the meet code enter there lol
RSB [31]

Answer:

bet

Explanation:

8 0
3 years ago
50PTS!! does anyone know how you turn in a section of your slides my teacher wants us to turn in only a portion of our google sl
HACTEHA [7]

Answer:

The best way to turn in one part of google slides, since you can't split the slide when turning assignments on classroom, is to copy and paste the part you want to turn in into another slide and submit that portion. I've done for many projects before and if you name the different slides (Ex: Assignment Portion One 5/6/20) so it won't get confusing with the multiple slides.

This is the best way I can think of, I hope I helped, and please correct me if there is a better way!

5 0
3 years ago
What is the decimal format of the binary IP address 11001110.00111010.10101010.01000011?
Neporo4naja [7]

Answer:

206.58.170.67 is the decimal format of the given binary IP address.

Explanation:

we can convert the binary number into decimal by the following procedure.

11001110 = 1x2⁷+1x2⁶+0x2⁵+0x2⁴+1x2³+1x2²+1x2¹+0x2⁰

            = 128 + 64 + 0 + 0 + 8 + 4 + 2 + 0

            =   206

00111010 = 0x2⁷+0x2⁶+1x2⁵+ 1x2⁴+1x2³+0x2²+1x2¹+0x2⁰

               = 0 + 0 + 32 + 16 + 8 + 0 + 2 + 0

               = 58

10101010 = 1x2⁷+0x2⁶+1x2⁵+ 0x2⁴+1x2³+0x2²+1x2¹+0x2⁰

              = 128 + 0 + 32 + 0 + 8 + 0 + 2 + 0

              = 170

01000011 =  0x2⁷+ 1x2⁶+ 0x2⁵+ 0x2⁴+ 0x2³+0x2²+1x2¹+1x2⁰

                = 0 + 64 + 0 + 0 + 0 + 0 + 2 + 1

                =  67

so, the IP address is becomes 206.58.170.67

7 0
3 years ago
​___________ are taking over as the primary communication platform for many business professionals. A. desktop computers B. lapt
Komok [63]

Answer:

Mobile devices .

Explanation:

For several industry professionals, mobile phones take over as the main communication channel because mobile devices are portable and easy for the purpose of communication or handle. With the help of mobile devices, they can easily interact with their employees, seniors, clients and other persons from which they want to interact or communicate.

  • Option A is not true about the scenario because it is not portable and it is not easy to handle as compared to the mobile devices. So, it's not the primary mode of communication.
  • Option B is not true about the scenario because it is portable but as compare to the mobile device, mobiles are more flexible, portable for primary communication.
  • Option c is not completely true because the professionals give primary preference to the calls than emails.
8 0
3 years ago
Read 2 more answers
The first step in the five-step process for problem solving is to ____.
DerKrebs [107]
Define  the problem..................
8 0
3 years ago
Read 2 more answers
Other questions:
  • WHOEVER HELPS ASAP GETS BRAINLiEST!!!!!!!!! :)
    15·2 answers
  • An audiophile is a person who _______.
    14·2 answers
  • Write a program with a main method that asks the user to enter an array of 10 integers. Your main method then calls each of the
    11·1 answer
  • Betrand Meyer developed the ______ programming language which is not type-safe because it violates the law of contravariance.
    9·1 answer
  • You are the administrator of the Sybex website. You are working when suddenly web server and network utilization spikes to 100 p
    15·1 answer
  • Which blog had legal content and is written mostly by lawyers
    14·2 answers
  • Rewritable (write, erase, write again) is known as _______.
    7·1 answer
  • Who do you guys ship lol bit with?
    14·2 answers
  • How to improve a poor game design puzzles
    11·1 answer
  • Enumerate five features of Microsoft excel 2016​
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!