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...
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?
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...
I am having an issue with the Java program with a tic tac toe. it isn't...
I am having an issue with the Java program with a tic tac toe. it isn't a game. user puts in the array and it prints out this. 1. Write a method print that will take a two dimensional character array as input, and print the content of the array. This two dimensional character array represents a Tic Tac Toe game. 2. Write a main method that creates several arrays and calls the print method. Below is an example of...
Game of Tic Tac Toe with the following conditions A point system where a  move that leads...
Game of Tic Tac Toe with the following conditions A point system where a  move that leads to a winning game is given 1 point, a move that leads to a tie is given 0 point, and a  lead to a losing game will get -1 point. Grid size of 5x5 A timer that can be set for how long a game can be played 5 symbols in a row to get a point Connected lines cannot be crossed (No diagonal lines)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT