三子棋
一.菜单。
二.游戏代码。
三.胜负判断。
四.三子棋实现。
下列代码所需要的头文件及宏定义
{
#define ROW 3
#define COL 3
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
}
一.菜单(菜单的实现,是否玩游戏)
int menu()
{
printf("--------------------------\n");
printf("--------1.开始游戏--------\n");
printf("--------0.退出游戏--------\n");
printf("--------------------------\n");
int choice = 0;
printf("请输入你的选择:");
scanf("%d", &choice);
return choice;
}
二.游戏代码
1.棋盘的打印
3*3的数组定义为空格:
void init(char chessBoard[ROW][COL])
{
for (int row = 0; row < ROW; row++)
{
for (int col = 0; col < COL; col++)
{
chessBoard[row][col] = ' ';
}
}
}
棋盘格子打印:
void chessBoard_print(char chessBoard[ROW][COL])
{
for (int row = 0; row < ROW; row++)
{
printf(" %c | %c | %c |\n", chessBoard[row][0], chessBoard[row][1], chessBoard[row][2]);
printf("---+---+---+\n");
}
}
2.玩家下棋
void playerMove(char chessBoard[ROW][COL])
{
while (1)
{
int row = 0;
int col = 0;
scanf("%d %d", &row, &col);
if (row < 0 || row >= ROW || col < 0 || col >= COL)
{
printf("您的坐标不在合法范围内 [0, 2],请重新输入:\n");
continue;
}
if (chessBoard[row][col] != ' ')
{
printf("请重新下旗子!\n");
continue;
}
chessBoard[row][col] = 'x';
break;
}
}
3.电脑下棋
void computerMove(char chessBoard[ROW][COL])
{
while (1)
{
int row = rand() % ROW;
int col = rand() % COL;
if (chessBoard[row][col] != ' ')
{
continue;
}
chessBoard[row][col] = 'o';
break;
}
}
4.判断是否此处可以落子
int isFull(char chessBoard[ROW][COL])
{
for (int row = 0; row < ROW; row++)
{
for (int col = 0; col < COL; col++)
{
if (chessBoard[row][col] == ' ')
{
return 0;
}
}
}
return 1;
}
5.主体
void game()
{
char chessBoard[ROW][COL] = { 0 };
init(chessBoard);
char winner = ' ';
while (1)
{
chessBoard_print(chessBoard);
playerMove(chessBoard);
winner = isWin(chessBoard);
if (winner != ' ')
{
break;
}
computerMove(chessBoard);
winner = isWin(chessBoard);
if (winner != ' ')
{
break;
}
}
chessBoard_print(chessBoard);
if (winner == 'x')
{
printf("恭喜您, 您赢了!\n");
}
else if (winner == 'o')
{
printf("你输了!\n");
}
else {
printf("平局\n");
}
}
三.胜负判断
char isWin(char chessBoard[ROW][COL])
{
for (int row = 0; row < ROW; row++)
{
if (chessBoard[row][0] != ' ' && chessBoard[row][0] == chessBoard[row][1] && chessBoard[row][0] == chessBoard[row][2]) //(判断行)
{
return chessBoard[row][0];
}
}
for (int col = 0; col < COL; col++)
{
if (chessBoard[0][col] != ' ' && chessBoard[0][col] == chessBoard[1][col] && chessBoard[0][col] == chessBoard[2][col])//(判断列)
{
return chessBoard[0][col];
}
}
if (chessBoard[0][0] != ' ' && chessBoard[0][0] == chessBoard[1][1] && chessBoard[0][0] == chessBoard[2][2])//(左交叉)
{
return chessBoard[0][0];
}
if (chessBoard[2][0] != ' ' && chessBoard[2][0] == chessBoard[1][1] && chessBoard[2][0] == chessBoard[0][2])//(右交叉)
{
return chessBoard[2][0];
}
if (isFull(chessBoard))//(和棋)
{
return 'heqi';
}
return ' ';
}
四.三子棋实现。
1.主函数
int main()
{
srand((unsigned int)time(NULL));
while (1)
{
int choice = menu();
if (choice == 1)
{
game();
}
else if (choice == 0)
{
printf("退出游戏!\n");
break;
}
else
{
printf("输入错误!请重新输入!\n");
continue;
}
}
return 0;
}
2.实现图示。
完整代码:
#define ROW 3
#define COL 3
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void init(char chessBoard[ROW][COL])//(3*3数组初始化为空格)
{
for (int row = 0; row < ROW; row++)
{
for (int col = 0; col < COL; col++)
{
chessBoard[row][col] = ' ';
}
}
}
void chessBoard_print(char chessBoard[ROW][COL])//(棋盘打印)
{
for (int row = 0; row < ROW; row++)
{
printf(" %c | %c | %c | \n", chessBoard[row][0], chessBoard[row][1], chessBoard[row][2]);
printf("---+---+---+\n");
}
}
void playerMove(char chessBoard[ROW][COL])//(玩家下棋)
{
while (1)
{
int row = 0;
int col = 0;
scanf("%d %d", &row, &col);
if (row < 0 || row >= ROW || col < 0 || col >= COL)
{
printf("您的坐标不在合法范围内 [0, 2],请重新输入:\n");
continue;
}
if (chessBoard[row][col] != ' ')
{
printf("请重新下旗子!\n");
continue;
}
chessBoard[row][col] = 'x';
break;
}
}
void computerMove(char chessBoard[ROW][COL])//(电脑下棋)
{
while (1)
{
int row = rand() % ROW;
int col = rand() % COL;
if (chessBoard[row][col] != ' ')
{
continue;
}
chessBoard[row][col] = 'o';
break;
}
}
int isFull(char chessBoard[ROW][COL])//(判断是否可以下子)
{
for (int row = 0; row < ROW; row++)
{
for (int col = 0; col < COL; col++)
{
if (chessBoard[row][col] == ' ')
{
return 0;
}
}
}
return 1;
}
char isWin(char chessBoard[ROW][COL])//(判断输赢)
{
for (int row = 0; row < ROW; row++)
{
if (chessBoard[row][0] != ' ' && chessBoard[row][0] == chessBoard[row][1] && chessBoard[row][0] == chessBoard[row][2]) //(行)
{
return chessBoard[row][0];
}
}
for (int col = 0; col < COL; col++)
{
if (chessBoard[0][col] != ' ' && chessBoard[0][col] == chessBoard[1][col] && chessBoard[0][col] == chessBoard[2][col])//(列)
{
return chessBoard[0][col];
}
}
if (chessBoard[0][0] != ' ' && chessBoard[0][0] == chessBoard[1][1] && chessBoard[0][0] == chessBoard[2][2])
{
return chessBoard[0][0];
}
if (chessBoard[2][0] != ' ' && chessBoard[2][0] == chessBoard[1][1] && chessBoard[2][0] == chessBoard[0][2])
{
return chessBoard[2][0];
}
if (isFull(chessBoard))
{
return 'heqi';
}
return ' ';
}
void game()
{
char chessBoard[ROW][COL] = { 0 };
init(chessBoard);
char winner = ' ';
while (1)
{
chessBoard_print(chessBoard);
playerMove(chessBoard);
winner = isWin(chessBoard);
if (winner != ' ')
{
break;
}
computerMove(chessBoard);
winner = isWin(chessBoard);
if (winner != ' ')
{
break;
}
}
chessBoard_print(chessBoard);
if (winner == 'x')
{
printf("恭喜您, 您赢了!\n");
}
else if (winner == 'o')
{
printf("你输了!\n");
}
else {
printf("平局\n");
}
}
int menu()//菜单
{
printf("--------------------------\n");
printf("--------1.开始游戏--------\n");
printf("--------0.退出游戏--------\n");
printf("--------------------------\n");
int choice = 0;
printf("请输入你的选择:");
scanf("%d", &choice);
return choice;
}
int main()
{
srand((unsigned int)time(NULL));
while (1)
{
int choice = menu();
if (choice == 1)
{
game();
}
else if (choice == 0)
{
printf("退出游戏!\n");
break;
}
else
{
printf("请重新输入!\n");
continue;
}
}
return 0;
}
(第一次正式博客,有错误请原谅!)