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...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has...
How to make a 2D array Tic Tac Toe game in C?
How to make a 2D array Tic Tac Toe game in C?
How to make tic tac toe game in javascript with vue.js
How to make tic tac toe game in javascript with vue.js
Write a LISP program to play the game Tic-Tac-Toe on a size 4x4 game board. Your...
Write a LISP program to play the game Tic-Tac-Toe on a size 4x4 game board. Your program must use min-max search and should be invoked by the function call: > (Tic-Tac-Toe) The game is single player, human vs the computer AI.
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!
Programming assignment (100 pts): In the C++ programming language write a program capable of playing Tic-Tac-Toe...
Programming assignment (100 pts): In the C++ programming language write a program capable of playing Tic-Tac-Toe against the user. Your program should use OOP concepts in its design. You can use ASCII art to generate and display the 3x3 playing board. The program should randomly decide who goes first computer or user. Your program should know and inform the user if an illegal move was made (cell already occupied). The program should also announce if one of the players wins...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a human, so each player makes their own moves. Follow the design below, creating the methods indicated and invoking them in the main program. Use a char array of size 9 as the board; initialize with the characters 0 to 8 so that it starts out looking something like the board on the left. 0|1|2 3|4|5 6|7|8 and then as moves are entered the board...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT