How to resolve the algorithm Tic-tac-toe step by step in the C programming language
Published on 7 June 2024 03:52 AM
How to resolve the algorithm Tic-tac-toe step by step in the C programming language
Table of Contents
Problem Statement
Play a game of tic-tac-toe. Ensure that legal moves are played and that a winning position is notified.
Tic-tac-toe is also known as:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Tic-tac-toe step by step in the C programming language
This C program simulates a game of Tic-Tac-Toe between the user and the computer.
Key Data Structures:
- b: A 3x3 array representing the game board. 0 indicates an empty cell, -1 indicates a computer move, and 1 indicates a user move.
Core Functions:
- check_winner(): Checks the game board for a winner. Returns 1 if the user wins, -1 if the computer wins, or 0 if no winner yet.
- showboard(): Displays the game board on the console.
- test_move(): Evaluates a potential move by the computer, considering all possible subsequent moves by the user. It returns a score indicating the potential outcome of the move.
- game(): The main game loop. It interacts with the user to get their move and simulates the computer's move. It returns a string indicating the winner or a draw.
Game Flow:
- Initialization: The game initializes the board and asks the user if they want to go first or second.
- Player Input: The user enters their move as a number corresponding to a position on the board. The program validates the input and updates the board.
- Computer Move: The computer evaluates all possible moves and chooses the one with the highest score using the test_move() function. It then updates the board.
- Check for Winner: After each move, the program checks if there is a winner or a draw using check_winner().
- Game Over: If a winner or a draw is detected, the game ends and the corresponding message is displayed.
Additional Features:
- Randomizing the first computer move for less predictability.
- Asking the user to play first or second.
- Continuous gameplay loop, allowing for multiple games.
Source code in the c programming language
#include <stdio.h>
#include <stdlib.h>
int b[3][3]; /* board. 0: blank; -1: computer; 1: human */
int check_winner()
{
int i;
for (i = 0; i < 3; i++) {
if (b[i][0] && b[i][1] == b[i][0] && b[i][2] == b[i][0])
return b[i][0];
if (b[0][i] && b[1][i] == b[0][i] && b[2][i] == b[0][i])
return b[0][i];
}
if (!b[1][1]) return 0;
if (b[1][1] == b[0][0] && b[2][2] == b[0][0]) return b[0][0];
if (b[1][1] == b[2][0] && b[0][2] == b[1][1]) return b[1][1];
return 0;
}
void showboard()
{
const char *t = "X O";
int i, j;
for (i = 0; i < 3; i++, putchar('\n'))
for (j = 0; j < 3; j++)
printf("%c ", t[ b[i][j] + 1 ]);
printf("-----\n");
}
#define for_ij for (i = 0; i < 3; i++) for (j = 0; j < 3; j++)
int best_i, best_j;
int test_move(int val, int depth)
{
int i, j, score;
int best = -1, changed = 0;
if ((score = check_winner())) return (score == val) ? 1 : -1;
for_ij {
if (b[i][j]) continue;
changed = b[i][j] = val;
score = -test_move(-val, depth + 1);
b[i][j] = 0;
if (score <= best) continue;
if (!depth) {
best_i = i;
best_j = j;
}
best = score;
}
return changed ? best : 0;
}
const char* game(int user)
{
int i, j, k, move, win = 0;
for_ij b[i][j] = 0;
printf("Board postions are numbered so:\n1 2 3\n4 5 6\n7 8 9\n");
printf("You have O, I have X.\n\n");
for (k = 0; k < 9; k++, user = !user) {
while(user) {
printf("your move: ");
if (!scanf("%d", &move)) {
scanf("%*s");
continue;
}
if (--move < 0 || move >= 9) continue;
if (b[i = move / 3][j = move % 3]) continue;
b[i][j] = 1;
break;
}
if (!user) {
if (!k) { /* randomize if computer opens, less boring */
best_i = rand() % 3;
best_j = rand() % 3;
} else
test_move(-1, 0);
b[best_i][best_j] = -1;
printf("My move: %d\n", best_i * 3 + best_j + 1);
}
showboard();
if ((win = check_winner()))
return win == 1 ? "You win.\n\n": "I win.\n\n";
}
return "A draw.\n\n";
}
int main()
{
int first = 0;
while (1) printf("%s", game(first = !first));
return 0;
}
You may also check:How to resolve the algorithm Non-decimal radices/Input step by step in the Erlang programming language
You may also check:How to resolve the algorithm Lah numbers step by step in the REXX programming language
You may also check:How to resolve the algorithm Barnsley fern step by step in the SPL programming language
You may also check:How to resolve the algorithm Create an HTML table step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Continued fraction/Arithmetic/G(matrix ng, continued fraction n1, continued fraction n2) step by step in the Tcl programming language