我们设计一个二维数组去储存当前的棋盘状态,然后利用Displayboard函数来打印起来,这里打印的要点是打印一行数据行,打印一行分割行,如果到了最后一行,就不打印分割行,打印数据行时,一次打印一个 " %c “,如果不是最后一个元素,就再打印一个”|",打印分割行时,一次打印一个"—“如果不是最后一个元素,就再打印一个"|"。
我们游戏的逻辑是初始化棋盘,打印棋盘,人下棋,打印棋盘,判断是否游戏结束,电脑下棋,打印棋盘,判断是否游戏结束,然后回到人下棋过程,结束游戏后,根据iswin函数的返回值来判断谁赢谁输还是平局。
判断输赢的逻辑是遍历行,遍历列,遍历主对角线,遍历反对角线,如果都判断不出输赢,那么就返回C表示游戏继续。
//game.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <Windows.h>
#define ROW 3
#define COL 3
void InitBoard(char arr[ROW][COL], int row, int col);
void Displayboard(char arr[ROW][COL], int row, int col);
void Playermove(char arr[ROW][COL], int row, int col);
void Computermove(char board[ROW][COL], int row, int col);
char iswin(char board[ROW][COL],int row,int col);
//要能返回四种状态
//玩家赢 *
//电脑赢 #
//平局 Q
//继续 C
void game();
//game.c
#include "game.h"
void game()
{
//棋盘数组
char board[ROW][COL];
//初始化棋盘
InitBoard(board, ROW, COL);
//打印棋盘
Displayboard(board, ROW, COL);
//玩家下棋
char ret;
while (1)
{
Playermove(board, ROW, COL);
Displayboard(board, ROW, COL);
ret = iswin(board, ROW, COL);
if (ret != 'C')
{
break;
}
Computermove(board, ROW, COL);
Sleep(1000);
Displayboard(board, ROW, COL);
ret = iswin(board, ROW, COL);
if (ret != 'C')
{
break;
}
Sleep(1000);
}
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("恭喜玩家获胜!\n");
}
if (ret == '#')
{
printf("恭喜电脑获胜\n");
}
if (ret == 'Q')
{
printf("旗鼓相当的对手\n");
}
}
void InitBoard(char arr[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
arr[i][j] = ' ';
}
}
}
void Displayboard(char arr[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
//打印数据行
for (int j = 0; j < col; j++)
{
printf(" %c ", arr[i][j]);
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
//打印分割行
if (i < row - 1)
{
for (int j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
}
printf("\n");
}
}
void Playermove(char board[ROW][COL], int row, int col)
{
printf("玩家走->");
int x;
int y;
while (1)
{
printf("请输入坐标->");
scanf("%d %d", &x, &y);
if (x <= ROW && x >= 1 && y <= COL && y >= 1)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("坐标被占用,请重新输入\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
}
void Computermove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
printf("电脑走->\n");
board[x][y] = '#';
break;
}
}
}
char iswin(char board[ROW][COL], int row, int col)
{
//判断输赢
//行
for (int i = 0; i < row; i++)
{
int countrow = 0;
for (int j = 0; j < col-1; j++)
{
if (board[i][j] == board[i][j + 1] && board[i][j]!=' ')
{
countrow++;
}
}
if (countrow == col - 1)
{
if (board[i][0] == '*')
{
return '*';
}
if (board[i][0] == '#')
{
return '#';
}
}
}
//列
for (int j = 0; j < col; j++)
{
int countcol = 0;
for (int i = 0; i < row - 1; i++)
{
if (board[i][j] == board[i + 1][j] && board[i][j]!=' ')
{
countcol++;
}
}
if (countcol == row - 1)
{
if (board[0][j] == '*')
{
return '*';
}
if (board[0][j] == '#')
{
return '#';
}
}
}
//主对角线
int count1 = 0;
for (int i = 0; i < row-1; i++)
{
if (board[i][i] == board[i + 1][i+1] && board[i][i]!=' ')
{
count1++;
}
}
if (count1 == col - 1)
{
if (board[0][0] == '*')
{
return '*';
}
if (board[0][0] == '#')
{
return '#';
}
}
//斜对角线
int count2 = 0;
for (int i = 0; i < row - 1; i++)
{
if (board[i][row - 1 - i] == board[i + 1][row - i - 2] && board[i][row-1-i]!=' ')
{
count2++;
}
}
if (count2 == col - 1)
{
if (board[0][col-1] == '*')
{
return '*';
}
if (board[0][col-1] == '#')
{
return '#';
}
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 'C';
}
}
}
return 'Q';
}
//sanziqi.c
#include "game.h"
void menu()
{
printf("*********************************\n");
printf("*********************************\n");
printf("*************1. play*************\n");
printf("*************0. exit*************\n");
printf("*********************************\n");
printf("*********************************\n");
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请选择\n");
scanf("%d", &input);
switch (input)
{
case 0:
printf("游戏结束\n");
break;
case 1:
system("cls");
game();
break;
default:
printf("选择错误,请重新输入\n");
break;
}
} while (input!=0);
}