A game of Tic-Tac-Toe.
★★★Noughts and crosses, also known as Tic-Tac-Toe is a game for two players. It is played on a 3x3 grid of squares. Player one plays with counters marked "X". Player two plays with counters marked "O". The aim of the game is to win by having placed three of your own counters adjacent to each other either horizontally, vertically or diagonally. On a players turn they place just one of their counters onto an empty square.
Write the program to play the game.
Remember to add a comment before a subprogram or selection statement to explain its purpose.
-1 indicates an empty square, 0 indicates an “O” and 10 indicates an “X”.
Numbers will make it easier to calculate if there is a winner.
get_move that:draw_board that:won that:play_game that:X wins., O wins. or Draw..play_game subprogram is called. 0 1 2
0 | |
---------
1 | |
---------
2 | |
Enter xy position:
11
0 1 2
0 | |
---------
1 |X|
---------
2 | |
Enter xy position:
21
…
X wins.
get_move
// Get the player move
static void get_move(int player)
{
---
bool valid_move = false;
int x = 0;
int y = 0;
---
// Check valid move
while (!valid_move)
{
---
valid_move = true;
Console.WriteLine("Enter xy position:");
string move = Console.ReadLine();
x = Convert.ToInt32(move[0].ToString());
y = Convert.ToInt32(move[1].ToString());
---
// Cannot move on top of another counter
if (board[x, y] != -1)
{
---
valid_move = false;
---
}
---
}
---
// Put a 10 on the board for X or O on the board for 0
if (player == 1)
{
---
board[x, y] = 10;
---
}
---
else
{
---
board[x, y] = 0;
---
}
}
draw_board
// Draw the board
static void draw_board()
{
---
Console.WriteLine(" 0 1 2");
// Draw rows
for (int y = 0; y < 3; y++)
{
---
Console.Write(y + " ");
// Draw columns
for (int x = 0; x < 3; x++)
{
---
// Output the counter X, O or a blank space
switch (board[x, y])
{
case -1:
Console.Write(" ");
break;
case 10:
Console.Write("X");
break;
case 0:
Console.Write("O");
break;
}
// Draw vertical line
if (x < 2)
{
Console.Write("|");
}
---
}
---
Console.WriteLine();
// Draw horizontal line
if (y < 2)
{
Console.WriteLine("---------");
}
---
}
}
won
// Check if a player has won
static bool won()
{
---
// Check horizontal & vertical win conditions
// Check all rows
for (int y = 0; y < 3; y++)
{
---
int htotal = 0;
int vtotal = 0;
// Check all columns
for (int x = 0; x < 3; x++)
{
---
htotal += board[x, y];
vtotal += board[y, x];
---
}
---
// Game is won with XXX or OOO
if (htotal == 30 || htotal == 0 || vtotal == 30 || vtotal == 0)
{
---
return true;
---
}
---
}
---
// Check left to right diagonal win condition
int dtotal = board[0, 0] + board[1, 1] + board[2, 2];
// Game is won with XXX or OOO
if (dtotal == 30 || dtotal == 0)
{
return true;
}
---
// Check right to left diagonal win condition
dtotal = board[2, 0] + board[1, 1] + board[0, 2];
// Game is won with XXX or OOO
if (dtotal == 30 || dtotal == 0)
{
return true;
}
---
return false;
}
play_game, main program
// Play the game
static void play_game()
{
---
int turns = 0;
int player = 1;
bool game_over = false;
draw_board();
// Play until the game is won or draw
while (!game_over)
{
---
turns++;
get_move(player);
draw_board();
// If the game has been won...
if (won())
{
---
game_over = true;
// ... output the winner
if (player == 1)
{
Console.WriteLine("X wins.");
}
else
{
Console.WriteLine("O wins.");
}
---
}
---
// If no player has won...
else
{
---
// ...check if it is a draw
if (turns == 9)
{
game_over = true;
Console.WriteLine("Draw.");
}
---
}
---
player++;
// Change the player back to player 1 after player 2's turn
if (player == 3)
{
player = 1;
}
---
}
}
---
public static void Main(string[] args)
{
---
// Empty squares have the value -1
for (int x = 0; x < 3; x++)
{
---
for (int y = 0; y < 3; y++)
{
---
board[x, y] = -1;
---
}
---
}
---
play_game();
}