At the beginning of the game, ask the human player to enter their name.
The human player will play against the computer program.
The player will use the X mark, and the computer program will use the O mark.
To keep it simple, the human player always plays first.
The game is played on a 3x3 grid. The 9 squares are identified by 2D coordinates shown below. For example, if the player wants to place a mark in the square in the middle, the player enters 1 and 1.
0, 0
0, 1
0, 2
1, 0
1, 1
1, 2
2, 0
2, 1
2, 2
The conventional rules of Tic-tac-toe apply.
Gameplay
Ask the player to enter their name.
Ask the player to enter the row number for the next play.
Ask the player to enter the column number for the next play.
The computer program generates a play.
Display the current board in text
If one player wins, break the loop. Otherwise, go to b.
Declare the winner. Game over.
Programming requirements
The board must be stored in a 3x3 2D array.
You must implement two methods and use them in your program.
static int checkWinner(char[,] board) This method takes a 3x3 2D array as input. This array stores the current board. The method checks if any player wins the game. If the human player wins, return 0. If the computer wins the game, return 1. Otherwise, return -1.
static int[] checkWinningPlay(char[,] board) This method takes a 3x3 2D array as input. This array stores the current board.This method checks if either player has a winning play.
If the human player has a winning play, return an array (0, i, j) where i and j are the coordinates of the winning square. For example, if the winning square is (0, 2), return (0, 0, 2). Otherwise, return (-1, -1, -1).
If the computer has a winning play, return an array (1, i, j) where i and j are the coordinates of the winning square. For example, if the winning square is (1, 0), return (1, 1, 0). Otherwise, return (-1, -1, - 1).
This method is a little tricky, and I'll explain it in class.
You may implement other methods. But checkWinner() and checkWinningPlay() are required.
Program logic. The following is for your reference. Your program does not need to follow this logic strictly. But you must implement and use the two methods checkWinner() and checkWinningPlay() in your code.
Ask the human player to enter their name.
Big loop
Display the board
Ask the player to enter the row number for the next play.
Ask the player to enter the column number for the next play.
Call checkWinner() to check if any player wins. If yes, record the winner and exit the loop. Otherwise, continue to the next step.
Call checkWinningMove() to check if any player has a winning play.
If computer has a winning play, occupy that square, record the winner, and exit the loop.
If human player has a winning play, occupy that square to prevent that play. Cointinue to the next step.
If there is no winning play, randomly choose an available square and occupy it. You can choose the first available square or find all 3 the available squares and randomly choose one. Continue to the next step.
Continue to step i.
Display the board.
Declare the winner.
What I'm asking you to do is to implement a decent computer Tic-tac-toe player who does not miss obvious winning or losing moves but does not use the perfect strategy either.
Make it a multi-round game and let the human player and computer take turns to make the first play. You decide how many rounds to play. Display the overall winner.
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference
and should not be submitted as is. We are not held liable for any misuse of the solutions.
Please see the frequently asked questions page
for further questions and inquiries.
Kindly complete the form.
Please provide a valid email address and we will get back to you within 24 hours.
Payment is through PayPal, Buy me a Coffee
or Cryptocurrency.
We are a nonprofit organization however we need funds to keep this organization operating
and to be able to complete our research and development projects.