Question

In: Computer Science

The objective of this assignment is to implement the tic-tac-toe game with a C program. The...

The objective of this assignment is to implement the tic-tac-toe game with a C program.
The game is played by two players on a board defined as a 5x5 grid (array). Each board
position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with
‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of
the board in turns. The objective is to place 5 consecutive markers of the same type in a line (a
line can be any row, any column or any diagonal). The first player who manages to place 5
markers in a line wins. The game is played until one of the players wins or until the board is full
with no player having 5 markers in a line (i.e., the result of the game is a draw).

Solutions

Expert Solution

For Taking Input and setting input position used alphabets as position(See Output Image)

Code: (Text File also included in End)


Output:


Files:


Code:

#include <stdio.h>
#include <stdbool.h> // Including Boolean

void fillGrid(char grid[5][5]){
   // Filling Grid
   int pos = 0;
   for (int i = 0; i < 5; i++) {
       for (int j = 0; j < 5; j++) {
           grid[i][j] = 'A' + pos; // Adding Alphabets for Reference
           pos += 1;
       }
   }
}

void printGrid(char grid[5][5]) { // Method for print grid
   for (int i = 0; i < 5; i++) {
       printf("\n");
       for (int j = 0; j < 5; j++) {
           printf("%c%s", grid[i][j], " ");
       }
   }
}

// Method for check winner
bool won(char symbol, char grid[5][5]) {
    bool diagWin = true; // Storing Status of Diagonal Win
    bool reverseDiagWin = true; // Storing Status of Reverse Diagonal Win
    for (int i = 0; i < 5; i++) { // Loop For check Win

        bool rowWin = true; // Storing Status of Row Win
        bool columnWin = true; // Storing Status of ColumnWin

        for (int j = 0; j < 5; j++) { // Checking if row or column are same
           rowWin = rowWin && (grid[i][j] == symbol);
           columnWin = columnWin && (grid[j][i] == symbol);
        }
      
        if (rowWin || columnWin) { // If Won Return True
           return true;
        }
      
            // Row and Column failed, Now testing Daigonal
        diagWin = diagWin && (grid[i][i] == symbol);
        reverseDiagWin = reverseDiagWin && (grid[i][5 - i - 1] == symbol);
    }
    if (diagWin || reverseDiagWin) { // If Won Return True
       return true;
    } else {
       return false;
    }

}

bool validInput(char grid[5][5], char input) { // Checking if Input is Valid
   for (int i = 0; i < 5; i++) {
       for (int j = 0; j < 5; j++) {
           if(input == grid[i][j]){
               return true;
           }
       }
   }
   return false;
}

int main() {
   char grid[5][5]; // Grid Array
   fillGrid(grid); // Fill Grid With Alphabets
   printGrid(grid); // Printing Grid

   bool turnX = true; // Storing Turn(X or O)
   int turn = 0; // Storing turn count for end game

        while (true) { // Infinite loop(play until win or game end)
           turn += 1;
            char symbol; // Symbol for turn
            if (turnX) {
               symbol = 'X';
            } else {
               symbol = 'O';
            }
            // Taking input and storing
            printf("\n%c%s", symbol, " Poisition[A to Y] -->");

            char val;
            while(true){ // Infinity loop for correct input
               scanf("%c", &val); // Taking input
               if(val == '\n'){ // Empty Line or Enter pressed
                   continue; // continue
               }else if(validInput(grid, val)){ // Input Valid
                   break; // Break loop
               }else{ // Invalid Invalid
                   printf("%s\n", "Wrong Input, Try Again!");
               }

            }
            int location = val - 'A'; // Converting input into int

            int row = location / 5; // Calcualting Row
            int col = location % 5; // Calcualting Column
            grid[row][col] = symbol; // Set Value
            printGrid(grid); // Print Grid
          
            if (won(symbol, grid)) { // Checking If Player Won
               printf("\n%s%c\n", "Winner: ", symbol);
               break;
            }
            if (turn == (5 * 5)) { // Checking if Game Over
               printf("\n%s\n", "Winner: None");
               break;
            }
            turnX = !turnX; // Changing Turm
        }
        return 0;
    }


Related Solutions

Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
In C# A Tic Tac Toe program that uses functions and arrays. The program should start...
In C# A Tic Tac Toe program that uses functions and arrays. The program should start by asking the player one for their name and then player two for their name. Then should print the board and ask the user what column and then what row they would like to put their x (or o, depending on the player) . Use the clear function to make it look as though the board is never repeating.. Thanks!
How to make tic tac toe game in javascript with vue.js
How to make tic tac toe game in javascript with vue.js
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed. I never said what type of code I needed in a previous question. I apologize and I can't go back and change it so here is the same question with more information Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players...
Implement the Tic-tac-toe game for variable board sizes, you may assume: 2 < s < 11,...
Implement the Tic-tac-toe game for variable board sizes, you may assume: 2 < s < 11, where s is the board size. Before the game starts the program will prompt for the board size. Note: the winning conditions are the same as the original Tic-tac-toe game in that you need to fill the entire row/column/diagonal to win. Here are a few sample runs. The output is a bit different so that we can handle two-digit coordinates consistently. We expect (but...
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional...
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row...
Assignment 2 Tic-Tac-Toe Game system design (10 marks) Introduction This document describes the functionalities of the...
Assignment 2 Tic-Tac-Toe Game system design Introduction This document describes the functionalities of the system and its design requirements. As first introduced in the Lab_Exercise_1, Tic-Tac-Toe is a simple game for two players. In this 1 assignment, you will implement a text-based Tic-Tac-Toe (TTT) game system that offers two game modes: 1) two human players playing against each other and 2) one human player playing against a built-in computer player in the game system. Your mission is to ensure the...
In a game of tic tac toe. How do you check if all the positions in...
In a game of tic tac toe. How do you check if all the positions in a double array are taken, the double arrays are used to store the x's and o's?
explain a pseudocode for tic tac toe in c++ between a computer and a player in...
explain a pseudocode for tic tac toe in c++ between a computer and a player in words
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications for a tic tac toe game object: class tictactoeGame { public: char boardConfig[3][3]; // two dimensional array stores current board configuration // Constructor: // set boardConfig[i][j] to be ' ' (the space character) // for 0<= i <= 2, 0<= j <= 2 tictactoeGame() { //fill this in } //put an 'X' character at the given location bool placeX(int x, int y) { //fill...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT