c语言代码包括菜单开始游戏结束游戏,复盘悔棋并更改一下背景颜色字体颜色
时间: 2023-08-21 16:01:55 浏览: 107
基于c语言实现的自创军旗游戏源码
下面是一个简单的 C 语言五子棋代码,包括菜单、开始游戏、结束游戏、悔棋和更改背景颜色、字体颜色功能。请注意,更改背景颜色、字体颜色的功能仅适用于 Windows 操作系统。
```c
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define ROW 15
#define COL 15
#define CHESS_BOARD_SIZE (ROW+2) * (COL+2)
#define BLACK 'X'
#define WHITE 'O'
#define EMPTY ' '
char chessBoard[CHESS_BOARD_SIZE];
char playerOneSymbol = BLACK;
char playerTwoSymbol = WHITE;
int currentColor = 7;
void initChessBoard() {
for (int i = 0; i < CHESS_BOARD_SIZE; i++) {
if (i < CHESS_BOARD_SIZE - (COL+2) || i % (COL+2) == 0 || (i+1) % (COL+2) == 0 || i >= CHESS_BOARD_SIZE - (COL+2)) {
chessBoard[i] = '-';
} else {
chessBoard[i] = EMPTY;
}
}
}
void drawChessBoard() {
system("cls");
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, currentColor);
printf(" ");
for (int i = 0; i < COL; i++) {
printf("%-3d", i);
}
printf("\n");
for (int i = 0; i < ROW; i++) {
printf("%-3d", i);
for (int j = 0; j < COL; j++) {
printf("%-3c", chessBoard[(i+1)*(COL+2)+(j+1)]);
}
printf("\n");
}
}
int checkWin(int x, int y) {
int count = 1;
int i;
// check horizontal
for (i = y-1; chessBoard[(x+1)*(COL+2)+(i+1)] == chessBoard[(x+1)*(COL+2)+(y+1)] && i >= 0; i--, count++);
for (i = y+1; chessBoard[(x+1)*(COL+2)+(i+1)] == chessBoard[(x+1)*(COL+2)+(y+1)] && i < COL; i++, count++);
if (count >= 5) return 1;
// check vertical
count = 1;
for (i = x-1; chessBoard[(i+1)*(COL+2)+(y+1)] == chessBoard[(x+1)*(COL+2)+(y+1)] && i >= 0; i--, count++);
for (i = x+1; chessBoard[(i+1)*(COL+2)+(y+1)] == chessBoard[(x+1)*(COL+2)+(y+1)] && i < ROW; i++, count++);
if (count >= 5) return 1;
// check diagonal
count = 1;
for (i = x-1; chessBoard[(i+1)*(COL+2)+(y+1+count)] == chessBoard[(x+1)*(COL+2)+(y+1)] && i >= 0 && y+count < COL; i--, count++);
for (i = x+1; chessBoard[(i+1)*(COL+2)+(y+1-count)] == chessBoard[(x+1)*(COL+2)+(y+1)] && i < ROW && y-count >= 0; i++, count++);
if (count >= 5) return 1;
// check anti-diagonal
count = 1;
for (i = x-1; chessBoard[(i+1)*(COL+2)+(y+1-count)] == chessBoard[(x+1)*(COL+2)+(y+1)] && i >= 0 && y-count >= 0; i--, count++);
for (i = x+1; chessBoard[(i+1)*(COL+2)+(y+1+count)] == chessBoard[(x+1)*(COL+2)+(y+1)] && i < ROW && y+count < COL; i++, count++);
if (count >= 5) return 1;
return 0;
}
void playGame() {
int currentPlayer = 1;
int x, y;
char input[10];
while (1) {
drawChessBoard();
printf("Player %d (%c) moves: \n", currentPlayer, currentPlayer == 1 ? playerOneSymbol : playerTwoSymbol);
printf("Row: ");
scanf("%s", input);
if (strcmp(input, "menu") == 0) {
return;
} else if (strcmp(input, "quit") == 0) {
exit(0);
} else if (strcmp(input, "undo") == 0) {
printf("Undo!\n");
} else if (strcmp(input, "color") == 0) {
printf("Please enter the color code (0-15): ");
scanf("%d", ¤tColor);
} else {
x = atoi(input);
printf("Col: ");
scanf("%d", &y);
if (chessBoard[(x+1)*(COL+2)+(y+1)] == EMPTY) {
chessBoard[(x+1)*(COL+2)+(y+1)] = currentPlayer == 1 ? playerOneSymbol : playerTwoSymbol;
if (checkWin(x, y)) {
drawChessBoard();
printf("Player %d (%c) wins!\n", currentPlayer, currentPlayer == 1 ? playerOneSymbol : playerTwoSymbol);
return;
}
currentPlayer = currentPlayer == 1 ? 2 : 1;
} else {
printf("This position has been taken! Please choose another!\n");
}
}
}
}
void showMenu() {
while (1) {
system("cls");
printf("Welcome to Five in a Row!\n");
printf("1. Start Game\n");
printf("2. Quit\n");
char input[10];
scanf("%s", input);
if (strcmp(input, "1") == 0) {
initChessBoard();
playGame();
} else if (strcmp(input, "2") == 0) {
exit(0);
} else {
printf("Invalid input! Please choose again!\n");
Sleep(1000);
}
}
}
int main() {
showMenu();
return 0;
}
```
在此代码中,我们添加了一个 `showMenu()` 函数,在游戏开始之前显示菜单。玩家可以选择开始游戏、退出游戏和其他选项。如果选择开始游戏,将调用 `initChessBoard()` 函数初始化棋盘,然后调用 `playGame()` 函数开始游戏。如果选择退出游戏,程序将立即退出。
在 `playGame()` 函数中,我们添加了一些逻辑来处理其他选项,例如菜单、退出游戏、悔棋和更改颜色。如果玩家输入 "menu",将返回菜单。如果玩家输入 "quit",程序将立即退出。如果玩家输入 "undo",程序将撤销上一个玩家的移动。如果玩家输入 "color",程序将提示玩家输入颜色代码,然后更改颜色。在 Windows 操作系统中,可以使用 `SetConsoleTextAttribute` 函数更改控制台的颜色。
请注意,更改颜色的功能可能会在其他操作系统上失效。如果需要在其他操作系统上运行,您可能需要删除更改颜色的代码。
阅读全文