Question

In: Computer Science

Can someone design a Tic Tac Toe game in java in the simplest way you can?...

Can someone design a Tic Tac Toe game in java in the simplest way you can? It should have

-the markers for the players are X and O

-2 players

-instructions on how to play the game in the start of the game

-show the Tic Tac Toe grid before asking each player for their input

-be on one java file

Thank you!!!!!!! This is for an intro class so please don't make it too complicated!

Solutions

Expert Solution

JAVA PROGRAM

import java.util.*;

class TicTacToe{//class TicTacToe
private char[][] mat;
private int m;
private int n;
public TicTacToe(int m,int n){//constructor
this.m=m;
this.n=n;
mat=new char[m][n];
}
public boolean setRowColumn(int row,int column,char ch){//method setRowColumn
if(row<0||row>=m||column<0||column>=n||mat[row][column]==ch){
System.out.println("Enter wrong input please try again!!");
return false;
}
mat[row][column]=ch;
return true;
}
public void displayboard(){//method displayboard
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(mat[i][j]=='X'||mat[i][j]=='O')
System.out.print("|"+mat[i][j]+"|");
else
System.out.print("| |");
}
System.out.println();
}
System.out.println();
}
public char WhoWin(){//method WhoWin
int count=1;
char ch=' ';
for(int i=0;i<m;i++){ //check row
for(int j=0;j<n-1;j++){
if(mat[i][j]==mat[i][j+1]){
count++;
ch=mat[i][j];
}
else
break;
}
if(count==m)
return ch;
count=1;
}

for(int i=0;i<n;i++){//check column
for(int j=0;j<m-1;j++){
if(mat[j][i]==mat[j+1][i]){
count++;
ch=mat[i][j];
}
else
break;
}
if(count==n)
return ch;
count=1;
}

for(int i=0,j=0;i<m-1&&j<n-1;i++,j++){//check diagonal
if(mat[i][j]==mat[i+1][j+1]){
count++;
ch=mat[i][j];
}
else
break;
}
if(count==m)
return ch;
count=1;
for(int i=0,j=n-1;i<m-1&&j>0;i++,j--){//check reverse diagonal
if(mat[i][j]==mat[i+1][j-1]){
count++;
ch=mat[i][j];
}
else
break;
}
if(count==m)
return ch;
count=1;

return ' ';
}
}
public class Main//class main
{
   public static void main(String[] args) {//method main
   Scanner s=new Scanner(System.in);
       System.out.print("Enter matrix size[rowSize columnSize] : ");
       int m=s.nextInt();
       int n=s.nextInt();
      
       TicTacToe T=new TicTacToe(m,n);
       int i,j;
       char ch=' ';
       do{
       T.displayboard();
       do{
       System.out.print("Enter player1[row column] : ");
       i=s.nextInt();
       j=s.nextInt();
       }while(!T.setRowColumn(i,j,'X'));
         
       T.displayboard();
       ch=T.WhoWin();
       if(ch=='X'||ch=='O')
       break;
  
       do{
       System.out.print("Enter player2[row column] : ");
       i=s.nextInt();
       j=s.nextInt();
       }while(!T.setRowColumn(i,j,'O'));
         
       ch=T.WhoWin();
       if(ch=='X'||ch=='O')
       break;
       }while(true);
      
       T.displayboard();
      
       if(ch=='X')
       System.out.println("Player1 Win!! ");
       else if(ch=='O')
       System.out.println("Player2 Win!! ");
      
   }
}

OUTPUT


Related Solutions

Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has...
How to make tic tac toe game in javascript with vue.js
How to make tic tac toe game in javascript with vue.js
In a game of tic tac toe. How do you check if all the positions in...
In a game of tic tac toe. How do you check if all the positions in a double array are taken, the double arrays are used to store the x's and o's?
If anyone can please write a code for a 5x5 tic tac toe game in matlab...
If anyone can please write a code for a 5x5 tic tac toe game in matlab I would greatly appreciate it. Its extra credit. PLEASE HELP ME :(
If you were to write a game of tic-tac-toe, you may store the representation of the...
If you were to write a game of tic-tac-toe, you may store the representation of the game board as a two dimensional list such as   [['X', '', 'X'], ['O', 'X', ''], ['', 'O', 'X']] where each sublist is a row in the board.   Empty strings ('') denote a space that does not yet have a value. Assuming this representation, write functions (contained in the same file) that do the following: a) Create a new empty tic-tac-toe board. This function should...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
How to make a 2D array Tic Tac Toe game in C?
How to make a 2D array Tic Tac Toe game in C?
Here is an interaction in a tic-tac-toe game, with user input in bold: >> gm =...
Here is an interaction in a tic-tac-toe game, with user input in bold: >> gm = Game.new('andy', 'mike') => #<Game:0x2e91d78 @players=[Andy, Mike]> >> gm.play_game('mike') Mike, enter your next O O-- --- --- Andy, enter your next X O-X --- --- Mike, enter your next O O-X -O- --- Andy, enter your next X move Bad move dude! You go again. O-X -O- --- Andy, enter your next X move O-X -OX --- Mike, enter your next O move O-X -OX...
Assignment 2 Tic-Tac-Toe Game system design (10 marks) Introduction This document describes the functionalities of the...
Assignment 2 Tic-Tac-Toe Game system design Introduction This document describes the functionalities of the system and its design requirements. As first introduced in the Lab_Exercise_1, Tic-Tac-Toe is a simple game for two players. In this 1 assignment, you will implement a text-based Tic-Tac-Toe (TTT) game system that offers two game modes: 1) two human players playing against each other and 2) one human player playing against a built-in computer player in the game system. Your mission is to ensure the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT