一、三子棋二、实现
1.玩家和电脑端进行对战2.展示 总结
一、三子棋
平时生活中玩的三子棋游戏。
(1)头文件:game.h
(2)测试类:test.c
(3)具体实现类:game.h
test():
#include "game.h" void menue() { printf("*******************************************n"); printf("************* 1. play **************n"); printf("************* 0. exit **************n"); printf("*******************************************n"); } //判断输赢 //1、玩家赢 2、电脑赢 3、平局 static void if_win(char board[ROW][COL], int row, int col) { int flag = 1; for (int i = 0; i <= row; i++) { for (int j = 0; j < col; j++) { if (board[i][j] != '*') { flag = 0; break; } } } } void game() { char board[ROW][COL] = {0}; //初始化数组 InitBoard(board, ROW, COL); //打印棋盘 //DisplayBoard(board, ROW, COL); char ret = 0; while (1) { //玩家下棋 player_move(board, ROW, COL); DisplayBoard(board, ROW, COL); //判断输赢 ret = is_win(board, ROW, COL); if (ret != 'C') { break; } //电脑下棋 computer_move(board, ROW, COL); DisplayBoard(board, ROW, COL); //判断输赢 ret = is_win(board, ROW, COL); if (ret != 'C') { break; } } if (ret == '*') { printf("玩家赢!n"); } else if (ret == '#') { printf("电脑赢!n"); } else { printf("平局!n"); } } void test() { int input = 0; srand ((unsigned int)time(NULL)); do { menue(); int command; printf("请选择->"); scanf("%d", &command); switch (command) { case 1: game(); break; case 0: printf("退出游戏n"); break; default: printf("请重新输入n"); } } while (input); } int main() { test(); return 0; }
game():
#include "game.h" //初始化棋盘 void InitBoard(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { board[i][j] = ' '; } } } //打印棋盘 void DisplayBoard(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j < col - 1) { printf("|"); } } printf("n"); if (i < row - 1) { for (int j = 0; j < col; j++) { if (j < col - 1) { printf("---|"); } else { printf("---"); } } printf("n"); } } } //玩家下棋 void player_move(char board[ROW][COL], int row, int col) { int x, y; printf("************* 玩家下棋 **************n"); while (1) { printf("请输入坐标->"); scanf("%d %d", &x, &y); if (x >= 1 && y >= 1 && x <= row && y <= col) { if (board[x-1][y-1] == ' ') { board[x-1][y-1] = '*'; break; } else { printf("您输入的坐标已被占用,请重新输入n"); } } else { printf("非法坐标,请重新输入n"); } } } //电脑下棋 void computer_move(char board[ROW][COL], int row, int col) { while (1) { int x = rand() % row; int y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } } //判断棋格是否满了 int if_full(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (board[i][j] == ' ') { return 0; } } } return 1; } //判断输赢 char is_win(char board[ROW][COL], int row, int col) { //三种情况赢:行、列、对角线 一种平局:棋盘满了 //判断行 for (int 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 (int j = 0; j < col; j++) { if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != ' ') { return board[0][j]; } } //判断对角线 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[0][2] != ' ') { return board[0][2]; } //判断平局 if (if_full(board, row, col) == 1) { return 'Q'; } else { return 'C'; } }2.展示
总结
多个数的情况下判断行列对角线的情况暂时还没写出来!!!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)