Drop links or images here to add them to the editor.

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.

Noughts and crosses

Make

Write the program to play the game.

Success Criteria

Remember to add a comment before a subprogram or selection statement to explain its purpose.

  1. In the main program, initialise the game board as a 3x3 array of -1 values.

-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.

Complete the subprogram called get_move that:

  1. Reads the xy position of where the current player wants to place their counter. E.g. 00 would be the top left corner, 22 would be the bottom right corner.
  2. Checks that a counter is only being placed on an empty square.
  3. Places a counter on a valid square.

Complete the subprogram called draw_board that:

  1. Outputs the board for the players using the format shown in the typical inputs and outputs below.

Complete the subprogram called won that:

  1. Checks if a player has won, returning true if they have or false if they have not.

Complete the subprogram called play_game that:

  1. Plays the game until either a player has won or the game is a draw. The game is a draw when a player cannot place a counter on the board.
  2. Outputs the result of the game: X wins., O wins. or Draw..

Complete the main program so that:

  1. The play_game subprogram is called.

Typical inputs and outputs from the program would be:

  0 1 2
0  | | 
---------
1  | | 
---------
2  | | 
Enter xy position:
11
  0 1 2
0  | | 
---------
1  |X| 
---------
2  | | 
Enter xy position:
21
…
X wins.
🆘 If you're really stuck, read the source code in the sample solution.
🆘 If you're really stuck, use this Parsons code sorting exercise
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();
}