Question

In: Computer Science

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)

The game ends when there is a tie or the timer runs out

The winner is the one with the most

Solutions

Expert Solution

import java.util.Scanner;

public class TicTakToe {
static char presentChoice='-';
static char game[][]={{'-','-','-','-','-'},{'-','-','-','-','-'},{'-','-','-','-','-'},{'-','-','-','-','-'},{'-','-','-','-','-'}};
static int count=0,m,n,m1,n1,m2,n2;
static Scanner sc;
//driver method to run the game
public static void main(String[] args)
{
   System.out.println("*******The Time Remaining to finish the game is 5 min*******\n");
   System.out.println("User1 - '0' & User2 - 'X'");
   sc=new Scanner(System.in);
   display(game);
   while(true)
   {
     
   while(true) if(input(1,'0')) break;
   display(game);
   gameFinishCheck();
     
   while(true) if(input(2,'X')) break;
   display(game);
   gameFinishCheck();
   if(count==10) break;
   }
   while(true){
       while(true) if(move(1)) break;
       display(game);
       presentChoice='0';
       gameFinishCheck();
       while(true) if(move(2)) break;
       display(game);
       presentChoice='X';
       gameFinishCheck();
   }
}
//to check game has finished or not
private static void gameFinishCheck() {
   if(validate(game,presentChoice))
   {
   if(presentChoice=='0')
   System.out.println("User1 won the game");
   else
   System.out.println("User2 won the game");
   System.exit(0);
   }
  
}
//to move the coin from one place to another place
private static boolean move(int i){
   try{
   char oldplace,newplace;
   System.out.println("\n\tUser"+i+" Turn");
   while(true){
   System.out.print("Select a position to move (row & column)\t:");
   m1=Integer.parseInt(sc.next());
   n1=Integer.parseInt(sc.next());
   if(movevalidate(m1,n1,game) && crctChoiceValidataion(i,game[m1][n1]))
   {
       oldplace=game[m1][n1];
       break;
   }
   else
   {
       System.out.println("\nEnter correct position\n");
   }
   }
   while(true)
   {
       System.out.print("Enter new position (row & column)\t\t:");
       m2=Integer.parseInt(sc.next());
       n2=Integer.parseInt(sc.next());
       if(validate(m2,n2,game))
       {
           newplace=game[m2][n2];
           break;
       }
       else
       {
           System.out.println("\nEnter correct position\n");
       }
   }
   game[m1][n1]=newplace;
   game[m2][n2]=oldplace;
   return true;
   }catch(Exception e){
       System.out.println("\nEnter correct input\n");
       return false;
   }
}
//to check weather user is picking correct choice for moving or not
private static boolean crctChoiceValidataion(int i, char c) {
   if(i==1 && c=='0') return true;
   else if(i==2 && c=='X') return true;
   else return false;
}
//to place the coin
private static boolean input(int i,char c){
   try{
   System.out.println("\n\tUser"+i+" Turn");
   System.out.print("Enter position (row & column)\t:");
   m=Integer.parseInt(sc.next());
   n=Integer.parseInt(sc.next());
   if(validate(m,n,game)){
   game[m][n]=c;
   presentChoice=c;
   count++;
   return true;
   }
   else{
   System.out.println("\nEnter correct position\n");
   return false;
   }
   }catch(Exception e){
       System.out.println("\nEnter correct input\n");
       return false;
   }
}
//this is to check game finished or not
private static boolean validate(char[][] game,char c) {
boolean result=false;

if((game[0][0]==c&&game[0][1]==c&&game[0][2]==c&&game[0][3]==c&&game[0][4]==c)) result= true;
if((game[1][0]==c&&game[1][1]==c&&game[1][2]==c&&game[1][3]==c&&game[1][4]==c)) result= true;
if((game[2][0]==c&&game[2][1]==c&&game[2][2]==c&&game[2][3]==c&&game[2][4]==c)) result= true;

if((game[0][0]==c&&game[1][0]==c&&game[2][0]==c&&game[3][0]==c&&game[4][0]==c)) result= true;
if((game[0][1]==c&&game[1][1]==c&&game[2][1]==c&&game[3][1]==c&&game[4][1]==c)) result= true;
if((game[0][2]==c&&game[1][2]==c&&game[2][2]==c&&game[3][2]==c&&game[4][2]==c)) result= true;

return result;

}
//to validate user input
private static boolean validate(int m, int n, char[][] game) {
if(m<0|| n<0||m>4 ||n>4||game[m][n]=='X'||game[m][n]=='0') return false;
else return true;
}
//to validate user move
private static boolean movevalidate(int m, int n, char[][] game) {
   if(m<0|| n<0||m>4 ||n>4) return false;
   else return true;
   }
//to print the game board
private static void display(char[][] game) {
System.out.println("\n------------------------------------\n");
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
System.out.print(game[i][j]);
if(j!=5) System.out.print("\t");
}
System.out.println("\n");
}
System.out.println("------------------------------------");
}
}


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...
Assume that the human player makes the first move against the computer in a game of Tic-Tac-Toe,
Assume that the human player makes the first move against the computer in a game of Tic-Tac-Toe, which has a 3 x 3 grid. Write a MATLAB function that lets the computer respond to that move. The function’s input argument should be the cell location of the human player’s move. The function’s output should be the cell location of the computer’s rst move. Label the cells as 1, 2, 3 across the top row; 4, 5, 6 across the middle...
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?
QUESTION: This is a tic-tac-toe game in react.js. Covert the following class-based components in React.js to...
QUESTION: This is a tic-tac-toe game in react.js. Covert the following class-based components in React.js to Functional Based component in React. //CODE import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; function calculateWinner(squares) { const lines = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6] ]; for (let i = 0; i < lines.length; i++) { const [a, b, c] = lines[i];...
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...
please create a tic tac toe game with python using only graphics.py library
please create a tic tac toe game with python using only graphics.py library
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...
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 :(
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed. I never said what type of code I needed in a previous question. I apologize and I can't go back and change it so here is the same question with more information Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT