Tic-Tac-Toe Simulator Create an application that simulates a game of tic-tac-toe
ID: 3715306 • Letter: T
Question
Tic-Tac-Toe Simulator
Create an application that simulates a game of tic-tac-toe. Figure 8-53 shows an example of the application’s form. The form shown in the figure uses eight large Label controls to display the Xs and Os. The application should use a two-dimensional Integer array to simulate the game board in memory. When the user clicks the New Game button, the application should step through the array, storing a random number in the range of 0 through 1 in each element. The number 0 represents the letter O, and the number 1 represents the letter X. The form should then be updated to display the game board. The application should display a message indicating whether player X won, player Y won, or the game was a tie.
The Form has to have 9 lablels that will display either Xs or Os randomly and a label that says either O wins, X wins, or Draw. there also has to be 2 buttons: 1 saying New Game which when pushed will display a board of random Xs and Os and say which wins...& a button saying exit. This has to be done in Visual Basic. It's not a game that you are able to play, it just generates random Xs and Os and says who wins.
Explanation / Answer
you didn't mentioned any programming Language
to develop this TicTacToe Game.
So i am providing the Java Application
Source Code:-
-------------------------
package com.samples;
import java.util.Scanner;
public class TicTacToeGame
{
Scanner sc = new Scanner(System.in);
static final int CROSS = 1, NOUGHT = 2;
static final int PLAYING = 0, DRAW = 1;
static final int CROSS_WON = 2,NOUGHT_WON = 3;
static int[][] board = new int[3][3];
static int currentState, currentPlayer,currntRow,currentCol;
public void initGame()
{
for (int i=0;i<3;++i)
{
for (int j=0;j<3;++j)
{
board[i][j] = 0;
}
}
currentState = PLAYING;
currentPlayer = CROSS;
}
public void playerMove(int theSeed)
{
boolean validInput = false;
do
{
if (theSeed == CROSS)
{
System.out.print("Player 'Venkanna', enter your move (row[1-3] column[1-3]): ");
}
else
{
System.out.print("Player 'Computer', enter your move (row[1-3] column[1-3]): ");
}
int row = sc.nextInt() - 1;
int col =sc.nextInt() - 1;
if (row>=0&&row<3 && col >= 0 && col < 3 && board[row][col] == 0)
{
currntRow = row;
currentCol = col;
board[currntRow][currentCol] = theSeed;
validInput = true;
}
else
{
System.out.println("This move at (" + (row + 1) + "," + (col + 1)+ ") is not valid. Try again...");
}
} while (!validInput);
}
/** Print the game board */
public void printBoard()
{
for(int i=0;i<3;++i)
{
for (int j=0;j<3;++j)
{
printCell(board[i][j]);
if (j!=3-1)
{
System.out.print("|"); // print vertical partition
}
}
System.out.println();
if (i != 3 - 1)
{
System.out.println("-----------");
}
}
System.out.println();
}
public static boolean isDraw()
{
for(int i=0;i<3;++i)
{
for (int j=0;j<3;++j)
{
if (board[i][j] == 0)
{
return false;
}
}
}
return true;
}
public void updateGame(int theSeed, int currentRow, int currentCol)
{
if (hasWon(theSeed, currentRow, currentCol))
{
currentState = (theSeed == CROSS) ? CROSS_WON : NOUGHT_WON;
}
else if (isDraw())
{ // check for draw
currentState = DRAW;
}
}
public static boolean hasWon(int theSeed, int currentRow, int currentCol)
{
return (board[currentRow][0] == theSeed
&& board[currentRow][1] == theSeed
&& board[currentRow][2] == theSeed
|| board[0][currentCol] == theSeed
&& board[1][currentCol] == theSeed
&& board[2][currentCol] == theSeed
|| currentRow == currentCol
&& board[0][0] == theSeed
&& board[1][1] == theSeed
&& board[2][2] == theSeed
|| currentRow + currentCol == 2
&& board[0][2] == theSeed
&& board[1][1] == theSeed
&& board[2][0] == theSeed);
}
//Driver Program
public static void main(String[] args)
{
TicTacToeGame obj=new TicTacToeGame();
obj.initGame();
do
{
obj.playerMove(currentPlayer);
obj.updateGame(currentPlayer, currntRow, currentCol);
obj.printBoard();
if (currentState == CROSS_WON)
{
System.out.println("venkanna won! Congratulation!");
}
else if (currentState == NOUGHT_WON)
{
System.out.println("'O' won! Congratulation!");
}
else if (currentState == DRAW)
{
System.out.println("It's a Draw! Bye!");
}
currentPlayer = (currentPlayer == CROSS) ? NOUGHT : CROSS;
} while (currentState == PLAYING); // repeat if not game-over
}
public static void printCell(int content) {
switch (content)
{
case 0:
System.out.print(" ");
break;
case NOUGHT:
System.out.print(" O ");
break;
case CROSS:
System.out.print(" X ");
break;
}
}
}
Sample Output:-
------------------------
Player 'Venkanna', enter your move (row[1-3] column[1-3]): 1
1
X | |
-----------
| |
-----------
| |
Player 'Computer', enter your move (row[1-3] column[1-3]): 2
3
X | |
-----------
| | O
-----------
| |
Player 'Venkanna', enter your move (row[1-3] column[1-3]): 0
1
This move at (0,1) is not valid. Try again...
Player 'Venkanna', enter your move (row[1-3] column[1-3]): 1
2
X | X |
-----------
| | O
-----------
| |
Player 'Computer', enter your move (row[1-3] column[1-3]): 3
3
X | X |
-----------
| | O
-----------
| | O
Player 'Venkanna', enter your move (row[1-3] column[1-3]): 1
3
X | X | X
-----------
| | O
-----------
| | O
venkanna won! Congratulation!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.