界面设置
void menu()
{
printf("************************\n");
printf("******* 1.play *******\n");
printf("******* 0.exit *******\n");
printf("************************\n");
}
棋盘设置
void Displayboard(char board[Row][Col], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf(" %c ",board[i][j]);
if (j < col - 1)
printf("|");
}printf("\n");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}printf("\n");
}
}
}
判断是否连成一条线
四个方向
整体代码
game.h
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define Row 3
#define Col 3
void Initboard(char board[Row][Col],int row,int col);
void Displayboard(char board[Row][Col], int row, int col);
void Computermove(char board[Row][Col],int row,int col);
void Playermove(char board[Row][Col],int row,int col);
char Iswin(char board[Row][Col], int row, int col);
int Isfull(char board[Row][Col], int row, int col);
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void Initboard(char board[Row][Col], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
// memset(&board[0][0], ' ', row*col*sizeof(board[0][0]));//方法同样可行
}
void Displayboard(char board[Row][Col], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf(" %c ",board[i][j]);
if (j < col - 1)
printf("|");
}printf("\n");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}printf("\n");
}
}
}
void Computermove(char board[Row][Col], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑走>\n");
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
void Playermove(char board[Row][Col], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家走>\n");
while (1)
{
printf("请输入坐标:>");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row&&y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("坐标被占用,请重新输入。\n");
}
}
else
printf("输入错误,请重新输入!\n");
}
}
char Iswin(char board[Row][Col], int row, int col)
{
int i = 0;
for (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 (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
{
return board[0][i];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
{
return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
{
return board[1][1];
}
if (Isfull(board, row, col))
{
return 'Q';
}
return 'C';
}
int Isfull(char board[Row][Col], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
text.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("************************\n");
printf("******* 1.play *******\n");
printf("******* 0.exit *******\n");
printf("************************\n");
}
void game()
{
int choice = 0;
char ret = 0;
char board[Row][Col] = { 0 };
Initboard(board, Row, Col);
Displayboard(board, Row, Col);
do
{
printf("选择1.玩家先手 2.电脑先手 >");
scanf("%d", &choice);
if (choice == 1 || choice == 2)
break;
else
printf("输入错误,请重新输入选择!");
} while (1);
while (choice==1)
{
Playermove(board,Row,Col);
ret = Iswin(board, Row, Col);
if (ret != 'C')
break;
Displayboard(board, Row, Col);
Computermove(board,Row,Col);
ret=Iswin(board,Row,Col);
if (ret != 'C')
break;
Displayboard(board, Row, Col);
}
while (choice == 2)
{
Computermove(board, Row, Col);
ret = Iswin(board, Row, Col);
if (ret != 'C')
break;
Displayboard(board, Row, Col);
Playermove(board, Row, Col);
ret = Iswin(board, Row, Col);
if (ret != 'C')
break;
Displayboard(board, Row, Col);
}
if (ret == '#')
{
printf("电脑赢\n");
}
else if (ret == '*')
{
printf("玩家赢\n");
}
else if (ret == 'Q')
{
printf("平局\n");
}
Displayboard(board, Row, Col);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
} while (input);
return 0;
}