思路: 完成一个三子棋程序,首先要有一个菜单,用于用户选择开始还是退出;然后就可以开始游戏了,在游戏最开始,首先进行一个小的猜数游戏,用于决定接下来的游戏是电脑先开始还是用户先开始;开始游戏以后,每当用户或者电脑走一步后都要显示棋盘现在的状态,然后进行判断,棋盘是否满了,电脑和用户的输赢, 接下来在进行继续落子,或者游戏结束,由用户决定是否开始开始下一轮。
在程序中要用到的函数有: void Menu();//菜单 void Init(char board[][COL], int row,int col);//初始化 int Guess();//猜数(用于决定谁先开始) void Show(char board[][COL], int row, int col);//显示棋盘 void PersonMove(char board[][COL], int row, int col);//用户落子 void ComputerMove(char board[][COL], int row, int col);//电脑落子 char Judge(char board[][COL], int row, int col);//判断游戏是否继续进行,输赢 void Game();//游戏过程
代码块及各部分功能:
在 Menu()函数中列出菜单,用printf()函数输出即可;
void Menu() { printf("\t\t\t*******************************************************\n\n\n"); printf("\t\t\t****************** 欢迎来到三子棋游戏 *****************\n\n\n"); printf("\t\t\t****************** 1、Play 2、Exit ******************\n\n\n"); printf("\t\t\t*******************************************************\n\n"); printf("请输入您的选择 > \n"); }在 Init(char board[][COL], int row,int col)函数中对棋盘进行初始化,棋盘使用二维数组表示,即将所有数组元素都赋为空格;
void Init(char board[][3],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] = ' '; } } }在 Guess()函数中进行猜数游戏,用于决定谁先开始,用rand()函数,进行rand()%2操作,保证输出的值是0或1;若用户猜对,返回1,用户先落子,若电脑猜对,返回0,电脑先落子;
int Guess() { int m = 0; int x = rand() % 2; printf("请您猜数(0或1)> "); scanf_s("%d", &m); if (x == m){ printf("恭喜,你猜对了,你先走!\n\n"); return 1; } else{ printf("不好意思,你猜错了,电脑先走!\n\n"); return 0; } }在Show(char board[][COL], int row, int col)函数中显示棋盘,用for循环输出棋盘的框架,在用户和电脑落子之后在输出相应棋子即可;
void Show(char board[][COL], int row, int col) { printf(" 1 | 2 | 3\n"); printf("---------------\n"); int i = 0; int j = 0; for (i = 1; i <= row; i++){ printf("%d |", i); for (j = 1; j <= col; j++){ printf("%c |", board[i - 1][j - 1]); } printf("\n"); printf("---------------\n"); } printf("\n"); }在PersonMove(char board[][COL], int row, int col)函数中,用户进行落子操作,对棋盘剩余空位和用户输入的位置进行判断,若用户输入位置无误且棋盘空位满足则落子,否则输出提示;
void PersonMove(char board[][COL], int row, int col) { while (1){ printf("请落子(x ,y) > "); int x = 0; int y = 0; scanf_s("%d %d", &x, &y); if (x<=0 || x>3 || y<=0 || y>3){ printf("您输入的位置有误,请重新输入!\n"); continue; } else if (board[x - 1][y - 1] != ' '){ printf("该位置已经被占了,请重新输入!\n"); continue; } else { board[x - 1][y - 1] = PERSON; break; } } }在 ComputerMove(char board[][COL], int row, int col)函数中,电脑进行落子操作,同样对棋盘剩余空位和随机产生的位置进行判断,满足条件即落子;
void ComputerMove(char board[][COL], int row, int col) { printf("电脑落子 > \n\n"); while (1){ int x = rand() % row; int y = rand() % col; if (board[x][y] == ' '){ board[x][y] = COMPUTER; break; } } }在Judge(char board[][COL], int row, int col)函数中判断游戏是否继续进行,以及输赢;若有一行,或一列,或斜着,都是同种类型的棋子,则该棋子拥有者胜利;若棋盘未满,则继续输入;若棋盘已满,还未分出胜负,则平局;
char Judge(char board[][COL], int row, int col) { int i = 0; int j = 0; for (i = 0; i < row; i++){ if (board[i][0] != ' '&&board[i][0] == board[i][1] && board[i][1] == board[i][2]){ return board[i][0]; } if (board[0][i] != ' '&&board[0][i] == board[1][i] && board[1][i] == board[2][i]){ return board[0][i]; } if (board[1][1] != ' '&&board[0][0] == board[1][1] && board[1][1] == board[2][2]){ return board[1][1]; } if (board[1][1] != ' '&&board[0][2] == board[1][1] && board[1][1] == board[2][0]){ return board[1][1]; } for (j = 0; j < col; j++){ if (board[i][j] == ' '){ return 'N'; } } } return 'F'; }在Game()函数中是游戏的基本过程,调用 Init(char board[][COL], int row,int col)函数对棋盘进行初始化;再调用Show(char board[][COL], int row, int col)函数显示棋盘,再调用Guess()函数进行猜数游戏,决定谁先开始,若用户猜对,返回1,用户先落子,若电脑猜对,返回0,电脑先落子,每次落子后进行显示棋盘(Show(char board[][COL], int row, int col)函数)和判断接下来的操作(Judge(char board[][COL], int row, int col)函数)
void Game() { srand((unsigned)time(NULL)); char result = 0; char board[ROW][COL]; Init(board, ROW, COL); printf("棋盘是这样的 > \n"); Show(board, ROW, COL); printf("游戏开始前先来个小游戏决定谁先走吧!\n\n"); int a = Guess(); do{ switch (a){ case 1: { PersonMove(board, ROW, COL); Show(board, ROW, COL); a = 0; break; } case 0: { ComputerMove(board, ROW, COL); Show(board, ROW, COL); a = 1; break; } default: break; } result = Judge(board, ROW, COL); if (result != 'N'){ break; } } while (1); switch (result){ case PERSON: printf("恭喜你,你赢了!\n"); break; case COMPUTER: printf("真遗憾,你输了!\n"); break; case 'F': printf("平局!\n"); break; default: break; } }完整代码:
#include<stdio.h> #include<windows.h> #include<time.h> #define ROW 3 #define COL 3 #define COMPUTER 'X' #define PERSON 'O' void Init(char board[][3],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] = ' '; } } } int Guess() { int m = 0; int x = rand() % 2; printf("请您猜数(0或1)> "); scanf_s("%d", &m); if (x == m){ printf("恭喜,你猜对了,你先走!\n\n"); return 1; } else{ printf("不好意思,你猜错了,电脑先走!\n\n"); return 0; } } void ComputerMove(char board[][COL], int row, int col) { printf("电脑落子 > \n\n"); while (1){ int x = rand() % row; int y = rand() % col; if (board[x][y] == ' '){ board[x][y] = COMPUTER; break; } } } void PersonMove(char board[][COL], int row, int col) { while (1){ printf("请落子(x ,y) > "); int x = 0; int y = 0; scanf_s("%d %d", &x, &y); if (x<=0 || x>3 || y<=0 || y>3){ printf("您输入的位置有误,请重新输入!\n"); continue; } else if (board[x - 1][y - 1] != ' '){ printf("该位置已经被占了,请重新输入!\n"); continue; } else { board[x - 1][y - 1] = PERSON; break; } } } void Show(char board[][COL], int row, int col) { printf(" 1 | 2 | 3\n"); printf("---------------\n"); int i = 0; int j = 0; for (i = 1; i <= row; i++){ printf("%d |", i); for (j = 1; j <= col; j++){ printf("%c |", board[i - 1][j - 1]); } printf("\n"); printf("---------------\n"); } printf("\n"); } char Judge(char board[][COL], int row, int col) { int i = 0; int j = 0; for (i = 0; i < row; i++){ if (board[i][0] != ' '&&board[i][0] == board[i][1] && board[i][1] == board[i][2]){ return board[i][0]; } if (board[0][i] != ' '&&board[0][i] == board[1][i] && board[1][i] == board[2][i]){ return board[0][i]; } if (board[1][1] != ' '&&board[0][0] == board[1][1] && board[1][1] == board[2][2]){ return board[1][1]; } if (board[1][1] != ' '&&board[0][2] == board[1][1] && board[1][1] == board[2][0]){ return board[1][1]; } for (j = 0; j < col; j++){ if (board[i][j] == ' '){ return 'N'; } } } return 'F'; } void Game() { srand((unsigned)time(NULL)); char result = 0; char board[ROW][COL]; Init(board, ROW, COL); printf("棋盘是这样的 > \n"); Show(board, ROW, COL); printf("游戏开始前先来个小游戏决定谁先走吧!\n\n"); int a = Guess(); do{ switch (a){ case 1: { PersonMove(board, ROW, COL); Show(board, ROW, COL); a = 0; break; } case 0: { ComputerMove(board, ROW, COL); Show(board, ROW, COL); a = 1; break; } default: break; } result = Judge(board, ROW, COL); if (result != 'N'){ break; } } while (1); switch (result){ case PERSON: printf("恭喜你,你赢了!\n"); break; case COMPUTER: printf("真遗憾,你输了!\n"); break; case 'F': printf("平局!\n"); break; default: break; } } void Menu() { printf("\t\t\t*******************************************************\n\n\n"); printf("\t\t\t****************** 欢迎来到三子棋游戏 *****************\n\n\n"); printf("\t\t\t****************** 1、Play 2、Exit ******************\n\n\n"); printf("\t\t\t*******************************************************\n\n"); printf("请输入您的选择 > \n"); } void main() { int quit = 1; int select = 1; do{ Menu(); scanf_s("%d", &select); switch (select){ case 1: Game(); break; case 2: quit = 0; break; default: printf("您的选择有误,请重新选择!"); break; } } while (quit); system("pause"); }运行结果: 选择1,开始游戏: 猜数游戏决定谁先走:
正式开始: 若选2,则退出: