C language implements a simple game of three chess

Have a certain understanding and understanding of the basic C language knowledge, and be able to solve some simple problems through C language programming. Use C language programming to complete a simple game "Three Connects".

Game introduction : The common 3x3 chessboard has two game players. The effect realized in this version is a man-machine battle. By playing chess one by one, if one side forms a three-character link, it is determined that this side wins. If the entire chessboard Covering is full, and has not yet formed a three-character link, then it is judged as a draw.

Coding idea : The chessboard is realized by a 3x3 array. Different players correspond to different characters. Every time a chess piece is placed in a position, it corresponds to assigning a value to the position. Every time a chess is played, it will be judged whether there is a winner and whether it has been A draw, or the game can continue, and so on until the end.
insert image description here

This coding uses C language, a total of five functions, each responsible for the corresponding functions.

void Menu()//Complete the planning of the entire game and interface
void Game()//The frame of the game integrates the whole idea of ​​the game
void playermove()//The player places the chess pieces
char judge()//Determine whether One side wins or draw
void computerMove()//Place chess pieces by computer
In coding, computer chess pieces are placed by generating a random integer. After generation, determine whether the position can be placed. If not, generate random numbers again.

#include<stdio.h>  

#pragma warning(disable:4996);

#include"C:\Users\尽欢\source\repos\ConsoleApplication2\game.h"

int main()
{
int select = 0;
while (1) {
Menu();
scanf("%d", &select);
switch (select)
{
case 1:
Game();
break;
case 2:
system("exit");
break;
default:
printf("输入有误!\n");
break;
}
return 0;
}
}

#ifndef __GAME_H__
#define __GAME_H__

#include <stdio.h>
#include <time.h>
#include <windows.h>
#pragma warning(disable:4996)

#define ROW 3
#define COL 3

#define P_COLOR 'X'
#define C_COLOR 'O'
#define NEXT 'N'
#define DRAW 'D'

void Menu();
void Game();
#endif
#include"game.h"
void Menu()
{
printf("   三子棋    ");
printf("-------------------");
printf("|  1.开始游戏  |");
printf("|  2.退出游戏  |");
printf("-------------------");
printf("请选择");
}

void Game()
{
srand((unsigned long)time(NULL));
char board[ROW][COL];
memset(board, ' ', sizeof(board));
char reselt = 'x';
do {
ShowBoard(board, ROW, COL);
PlayerMove(board, ROW, COL);
result = Judge(board, ROW, COL);
if (result != NEXT) {
break;
}
ComputerMove(board, ROW, COL);
result = Judge(board, ROW, COL);
if (result != NEXT) {
break;
}
} while (1);

if (P_COLOR == result) {
printf("你赢了!\n");
}
else if (C_COLOR == result) {
printf("你输了!\n");
}
else {
printf("和棋!\n");

}
ShowBoard(board, ROW, COL);
}

}

void playermove(char board[][COL], int row, int col)
{
int x = 0;
int y = 0;
int quit = 0;
while (1) {
printf("请输入你的位置:");
scanf("%d %d", x, y);
if (x < 1 || x>3 || y < 1 || y>3)
{
printf("你输入的位置有误!\n");
continue;
}
else if (board[x - 1][y - 1] != ' ')
{
printf("该位置已经被占用\n");
}
else
{
board[x - 1][y - 1] = P_COLOR;
break;
}
}
}

char Judge(char board[][COL], int ROW, int col)
{
for (int i = 0;i < row;i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
return board[i][0];
}
for (int j = 0; j < col; j++) {
if (board[0][j] == board[1][j] && \
board[1][j] == board[2][j] && \
board[0][j] != ' ') {
return board[0][j];
}
}

if (board[0][0] == board[1][1] && \
board[1][1] == board[2][2] && \
board[0][0] != ' ') {
return board[1][1];
}

if (board[0][2] == board[1][1] && \
board[1][1] == board[2][0] && \
board[1][1] != ' ') {
return board[1][1];
}

for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (board[i][j] == ' ') {
return NEXT;
}
}
}

return DRAW;
}

void ComputerMove(char board[][COL], int row, int rol)
{
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = C_COLOR;
break;
}
}
}

Guess you like

Origin blog.csdn.net/qq_45742383/article/details/113409299