Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I really need this help. Please use C++ and use the starting template that is al

ID: 3779521 • Letter: I

Question

I really need this help. Please use C++ and use the starting template that is already provided!

Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you must copy and paste to cloud9. Do not change the provided complete functions, or the function stub headers / return values.

Currently, if the variables provided in main are commented out, the program will compile. Complete the specifications for each function. As you develop the program, implement one function at a time, and test that function. The provided comments provide hints as to what each function should do and when each should be called. Add variables where you see fit.

Implementation Strategies:

The template has variables and two global constants for you to utilize.

The template has string literals for winning or tie game output in comments with provided file.

The template has comments to help you develop the necessary algorithm for 2 users playing tic-tac-toe on a computer. Use these comments along with the function descriptions below to help develop your program. One or more lines of your code should exist below each comment. Remove the TODO part when you have completed that step.

DO NOT try to implement the entire game at once. Instead, develop and test one function at a time. Understand how to walk through your code by hand as well as executing it in unit tests.

The system will test your functions as you attempt to complete each. Use the feedback and walk through your code to fix problems.

Submit and achieve a full score on each function before implementing the main game algorithm.

Tie Game Example:

Starting Template:

Explanation / Answer

Answer:

import java.util.*;

public class TicTacToeGame
{
private int counter;
private char location[]=new char[10];
private char player;
  
  
public static void main(String args[])
{
String ch;
TicTacToeGame Toe=new TicTacToeGame();
do{
Toe.beginBoard();
Toe.play_game();
System.out.println ("Press Y to play the game again ");
Scanner in =new Scanner(System.in);
ch=in.nextLine();
System.out.println("ch value is "+ch);
}while (ch.equals("Y"));
  
  
}
public void beginBoard()
{
  
char locationdef[] = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9'};
int i;
counter = 0;
player = 'X';
for (i=1; i<10; i++) location[i]=locationdef[i];
presentBoard();
  
  
}
public String presentBoard()
{
System.out.println( " " );
System.out.println( " " );
System.out.println( " " + location [1] + " | " +location [2]+ " | " +location [3]);
System.out.println( " | | " );
System.out.println( " ___|____|___ " );
System.out.println( " " +location [4]+ " | " +location [5]+ " | " +location [6]);
System.out.println( " | | " );
System.out.println( " ___|____|___ " );
System.out.println( " " +location [7]+ " | " +location [8]+ " | " +location [9]);
System.out.println( " | | " );
System.out.println( " | | " );
System.out.println( " " );
return "presentBoard";
}
  
public void play_game()
{
int spot;
char blank = ' ';
  
System.out.println( "Player " + getPlayer() +" will go first and be the letter 'X'" );
  
do {
presentBoard();
  
System.out.println( " Player " + getPlayer() +" choose a location." );
  
boolean posTaken = true;
while (posTaken) {
  
Scanner in =new Scanner (System.in);
spot=in.nextInt();
posTaken = checklocation(spot);
if(posTaken==false)
location[spot]=getPlayer();
}
  
System.out.println( "Nice move." );
  
presentBoard();
  
nextPlayer();
}while ( getWinner() == blank );
  
}
  
public char getWinner()
{
char Winner = ' ';
  
  
if (location[1] == 'X' && location[2] == 'X' && location[3] == 'X') Winner = 'X';
if (location[4] == 'X' && location[5] == 'X' && location[6] == 'X') Winner = 'X';
if (location[7] == 'X' && location[8] == 'X' && location[9] == 'X') Winner = 'X';
if (location[1] == 'X' && location[4] == 'X' && location[7] == 'X') Winner = 'X';
if (location[2] == 'X' && location[5] == 'X' && location[8] == 'X') Winner = 'X';
if (location[3] == 'X' && location[6] == 'X' && location[9] == 'X') Winner = 'X';
if (location[1] == 'X' && location[5] == 'X' && location[9] == 'X') Winner = 'X';
if (location[3] == 'X' && location[5] == 'X' && location[7] == 'X') Winner = 'X';
if (Winner == 'X' )
{System.out.println("Player1 wins the game." );
return Winner;
}
  
  
if (location[1] == 'O' && location[2] == 'O' && location[3] == 'O') Winner = 'O';
if (location[4] == 'O' && location[5] == 'O' && location[6] == 'O') Winner = 'O';
if (location[7] == 'O' && location[8] == 'O' && location[9] == 'O') Winner = 'O';
if (location[1] == 'O' && location[4] == 'O' && location[7] == 'O') Winner = 'O';
if (location[2] == 'O' && location[5] == 'O' && location[8] == 'O') Winner = 'O';
if (location[3] == 'O' && location[6] == 'O' && location[9] == 'O') Winner = 'O';
if (location[1] == 'O' && location[5] == 'O' && location[9] == 'O') Winner = 'O';
if (location[3] == 'O' && location[5] == 'O' && location[7] == 'O') Winner = 'O';
if (Winner == 'O' )
{
System.out.println( "Player2 wins the game." );
return Winner; }
  

for(int i=1;i<10;i++)
{
if(location[i]=='X' || location[i]=='O')
{
if(i==9)
{
char Draw='D';
System.out.println(" Game is stalemate ");
return Draw;
}
continue;
}
else
break;
  
}
  
return Winner;
}
  
public boolean checklocation(int spot)
{
  
  
if (location[spot] == 'X' || location[spot] == 'O')
{
System.out.println("That location is already taken, please choose another");
return true;
}
else {
return false;
}
  
  
  
}
  
  
  
public void nextPlayer()
{
if (player == 'X')
player = 'O';
else player = 'X';
  
}
  
public String getTitle()
{
return "Tic Tac Toe" ;
}
  
public char getPlayer()
{
return player;
}
  
}