三子棋

mac2024-04-08  35

三子棋其实不难,别被代码吓住了!这种代码关键考验的是思路与细心程度

先声明头文件
#ifndef GAME_H #define GAME_H #include<stdio.h> #include<time.h> #pragma warning(disable:4996) #define COL 3 #define ROW 3 void start();//欢迎界面 void game(); void initial(char table[3][3], int cow, int row);//初始化界面 void computer(char table[3][3], int cow, int row);//电脑走 void showtable(char table[3][3], int col, int row);//展示界面 char judge(char table[3][3]);//判输赢 #endif // GAME_H
写主函数
#include"game.h" int main() { start(); int choose; int flag = 0; while (!flag) { printf("please choose :>"); scanf("%d", &choose); switch (choose) { case 1: game(); //printf("Are you want to play again?\n"); break; case 0: flag = 1; printf("ByeBye!\n"); break; default: printf("enter error! please enter again!\n"); break; } } return 0; }
写出其他的调用函数
#include"game.h" void start() { printf("###########################\n"); printf("##### Welcome to join #####\n"); printf("###########################\n"); printf("### 1.play game 0.exit ###\n"); printf("###########################\n"); } void game() { srand((unsigned)time(NULL));//随机种子 char table[3][3]; initial(table, COL, ROW); int x, y; int first = rand() % 2; if (first == 0)//如果first是0,电脑先走,反之玩家先走 { printf("computer go before>\n"); computer(table, COL, ROW); showtable(table, COL, ROW); } char result = 'N'; while (result == 'N') { printf("choose your coordinate :"); scanf("%d%d", &x, &y); x--; y--; if (x < 0 || x>2 || y < 0 || y>2) { printf("enter error! please again:>\n"); continue; } if (table[x][y] != ' ') { printf(" there plase is occupied! please again:>\n"); continue; } table[x][y] = 'X'; computer(table, COL,ROW); showtable(table, COL, ROW); result = judge(table); switch (result) { case 'N': break; case 'C': printf("\nsorry! the computer won!\n\n"); break; case 'P': printf("you are won!\n"); break; case 'M': printf("just so-so!\n"); break; default:printf("3"); break; } } start(); printf("Are you again?\n"); } /*初始化棋盘*/ void initial(char table[3][3], int col, int row) { printf(" | 1 | 2 | 3 |\n"); printf("------------------\n"); for (int i = 0; i < col; i++) { printf(" %d |", i + 1); for (int j = 0; j < row; j++) { table[i][j] = ' '; printf(" %c |", table[i][j]); } printf("\n------------------\n"); } } /*电脑走*/ void computer(char table[3][3], int cow, int row) { srand((unsigned)time(NULL)); int x = 0, y = 0, ch = 0; for (int i = 0; i < COL; i++) { if (ch == 1) { break; } for (int j = 0; j < ROW; j++) { if (table[i][j] == ' ') { ch = 1; break; } } } while (ch == 1) { x = rand() % 3; y = rand() % 3; if (table[x][y] == ' ') { table[x][y] = 'O'; break; } } } /*判断输赢*/ char judge(char table[3][3]) { char result='M'; for (int i = 0; i < COL; i++) { if (result == 'N') { break; } for (int j = 0; j < ROW; j++) { if (table[i][j] == ' ') { result = 'N'; break; } } } for (int i = 0; i < COL; i++) { if (table[i][0]!=' '&& table[i][0]== table[i][1] && table[i][0] == table[i][2]) { if (table[i][0]=='X') { result = 'P'; break; } else { result = 'C'; break; } } if (table[0][i] != ' ' && table[0][i] == table[1][i] && table[0][i] == table[2][i]) { if (table[0][i] == 'X') { result = 'P'; break; } else { result = 'C'; break; } } } if (table[0][0] != ' ' && table[0][0] == table[1][1] && table[0][0] == table[2][2]) { if (table[0][0] == 'X') { result = 'P'; } else { result = 'C'; } } if (table[0][2] != ' ' && table[0][2] == table[1][1] && table[0][2] == table[2][0]) { if (table[2][0] == 'X') { result = 'P'; } else { result = 'C'; } } return result; } /*展示页面*/ void showtable(char table[3][3], int col, int row) { printf(" | 1 | 2 | 3 |\n"); printf("------------------\n"); for (int i = 0; i < col; i++) { printf(" %d |", i + 1); for (int j = 0; j < row; j++) { printf(" %c |", table[i][j]); } printf("\n------------------\n"); } }
最新回复(0)