Hey guys, I need help writing a Tic-Tac-Toe game programmed in Java. The game has to be a 1D (One-Dimension) array board NOT 2D. It has to be a human plays against the computer type of Tic-Tac-Toe. Here is the requirements for the program. PLEASE NO COPY AND PASTE ANSWER. Thank you in advance!
You will develop a program in which a human plays
against the computer.
1. Validate user input at every opportunity.
a. Do not allow number entries less than 0
b. Do not allow number entries greater than 8
c. Do not allow non-numeric entries
2. Do not use global variables in the development of the
program
3. You may use global constants in the development of the
program
4. Use one-dimensional arrays to keep track of the
game:
a. Computer moves
b. Human moves
5. You must use functions to pass arrays and implement
other program requirements.
6. The program must be developed using functions so that the main()
function consists mostly of function calls
7. Computer must be aggressive and take every opportunity to win
the game, block the user from winning and if the human makes a
mistake, then the computer will capitalize on it and win if
possible
8. The main() function must use a loop to keep the user in
the program until he/she wants to quit.
9. You may not use infinite loops: for(;;)
or while(true)
10. You may not use the break statement to exit loops
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( "\n\n" );
System.out.println( "\n\n" );
System.out.println( "\n\n\t\t" + location [1] + " | " +location
[2]+ " | " +location [3]);
System.out.println( " \t\t | | " );
System.out.println( " \t\t ___|____|___ " );
System.out.println( "\n\n\t\t" +location [4]+ " | " +location [5]+
" | " +location [6]);
System.out.println( " \t\t | | " );
System.out.println( " \t\t ___|____|___ " );
System.out.println( "\n\n\t\t" +location [7]+ " | " +location [8]+
" | " +location [9]);
System.out.println( " \t\t | | " );
System.out.println( " \t\t | | " );
System.out.println( "\n\n" );
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( "\n\n 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;
}
}
Hey guys, I need help writing a Tic-Tac-Toe game programmed in Java. The game has to...
(Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enumeration to represent the value in each cell of the array. The enumeration’s constants should be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY. Allow two human players. Wherever the first player moves, place an X...
I need to create a Tic Tac Toe program in C++. These are the requirements Write a program that allows the computer to play TicTacToe against a human player or allow two human players to play one another. Implement the following conditions: The player that wins the current game goes first in the next round, and their symbol is X. The other player will be O. Keep track of the number of games played, wins, and draws for each player....
Please make this into one class. JAVA Please: Tic Tac Toe class - Write a fifth game program that can play Tic Tac Toe. This game displays the lines of the Tic Tac Toe game and prompts the player to choose a move. The move is recorded on the screen and the computer picks his move from those that are left. The player then picks his next move. The program should allow only legal moves. The program should indicate when...
tic-tac-toe game. Add functionality to the program so when the button is clicked for the AI to take a turn, a heuristic is applied for each of the possible moves, a possible move is selected and the game state and GUI are properly updated. Once complete, the program should be able to play a single game of tic-tac-toe with the user. C#
Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game. The objective of this project is to demonstrate your understanding of various programming concepts including Object Oriented Programming (OOP) and design. Tic Tac Toe is a two player game. In your implementation one opponent will be a human player and the other a computer player. ? The game is played on a 3 x 3 game board. ? The first player is known as...
1. Use Turtle Graphics to create a tic tac toe game in Python. Write a Python program that allows for one player vs computer to play tic tac toe game, without using turtle.turtle
PYTHON Exercise 2. Tic-Tac-Toe In this exercise we are going to create a Tic-Tac-Toe game. 1. Create the data structure – Nine slots that can each contain an X, an O, or a blank. – To represent the board with a dictionary, you can assign each slot a string-value key. – String values in the key-value pair to represent what’s in each slot on the board: ■ 'X' ■ 'O' ■ ‘ ‘ 2. Create a function to print the...
EXTRA CREDIT 1: Tic Tac Toe (Please use c++ code) also please verify it works because I've received wrong answers in the past. Thank You 99 17 30 12 EXTRA CREDIT 1: Tic Tac Toe (10 points) Write a console program that plays Tic Tac Toe with the user. The program displays a 3 by 3 grid, which is initially blank. When the player takes a turn, an X is added to the grid. When the computer takes a turn,...
I need help developing this app using java in android studio Assignment: Tic-Tac-Toe app User interface Operation The app allows the user to play a game of Tic-Tac-Toe. The user can click the New Game button at any time to start a new game. The app displays other messages to the user as the game progresses such as (1) whose turn it is, (2) if a player wins, and (3) if the game ends in a tie. Specifications The app...
Assignment C++: Rock-Scissor-Paper & Tic-Tac-Toe i need you to write two different program for RSP and TTT: R-S-P Requirement: - write one program that mimics the Rock-Scissor-Paper game. This program will ask user to enter an input (out of R-S-P), and the computer will randomly pick one and print out the result (user wins / computer wins). - User's input along with computer's random pick will be encoded in the following format: -- user's rock: 10 -- user's scissor: 20...