博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
五子棋
阅读量:3950 次
发布时间:2019-05-24

本文共 2949 字,大约阅读时间需要 9 分钟。

#define _CRT_SECURE_NO_WARNINGS#include 
#include
#include
//五子棋 int end; // 用于跳出主函数里边的循环,结束游戏char chess[5][5]; // 棋盘//初始化棋盘void init() { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { chess[i][j] = ' '; } }}//打印棋盘void table() { printf("+---+---+---+---+---+\n"); for (int i = 0; i < 5; i++) { printf("| %c | %c | %c | %c | %c |\n", chess[i][0], chess[i][1], chess[i][2], chess[i][3], chess[i][4]); printf("+---+---+---+---+---+\n"); }}//玩家输入void PlayerInput() { while (1) { int x, y; scanf("%d %d", &x, &y); if (x < 0 || x>4 || y < 0 || y>4) { printf("你输入了个什么东西,瞅瞅再来输入!\n"); } if (chess[x][y] == 'x'|| chess[x][y] == 'o') { printf("这里已经有棋子了,你还是重新找个地方吧!\n"); } else{ chess[x][y] = 'x'; break; } }}//电脑输入void ComputerInput() { while (1) { int x, y; x = rand() % 5; y = rand() % 5; if (x < 0 || x>4 || y < 0 || y>4) { ; } if (chess[x][y] == 'x'|| chess[x][y] == 'o') { ; } else { chess[x][y] = 'o'; break; } } }// 判断是不是玩家赢了int Pwin() { int count = 0; for (int i = 0; i < 5; i++) { if (chess[i][0] == 'x'&& chess[i][1] == 'x' && chess[i][2] == 'x' && chess[i][3] == 'x' && chess[i][4] == 'x') { count = 1; } } for (int i = 0; i < 5; i++) { if (chess[0][i] == 'x'&& chess[1][i] == 'x' && chess[2][i] == 'x' && chess[3][i] == 'x' && chess[4][i] == 'x') { count = 1; } } if (chess[0][0] == 'x'&& chess[1][1] == 'x' && chess[2][2] == 'x' && chess[3][3] == 'x' && chess[4][4] == 'x') { count = 1; } if (chess[0][4] == 'x'&& chess[1][3] == 'x' && chess[2][2] == 'x' && chess[3][1] == 'x' && chess[4][0] == 'x') { count = 1; } return count;}// 判断是不是电脑赢了int Cwin() { int count = 0; for (int i = 0; i < 5; i++) { if (chess[i][0] == 'o'&& chess[i][1] == 'o' && chess[i][2] == 'o' && chess[i][3] == 'o' && chess[i][4] == 'o') { count = 1; } } for (int i = 0; i < 5; i++) { if (chess[0][i] == 'o'&& chess[1][i] == 'o' && chess[2][i] == 'o' && chess[3][i] == 'o' && chess[4][i] == 'o') { count = 1; } } if (chess[0][0] == 'o'&& chess[1][1] == 'o' && chess[2][2] == 'o' && chess[3][3] == 'o' && chess[4][4] == 'o') { count = 1; } if (chess[0][4] == 'o'&& chess[1][3] == 'o' && chess[2][2] == 'o' && chess[3][1] == 'o' && chess[4][0] == 'o') { count = 1; } return count;}// 判断最终获胜者void IsWinner() { // 1 和棋 // 2 玩家赢了 // 3 电脑赢了 int x = Pwin(); if (x == 1) { printf("厉害厉害,你是大哥,电脑都能下不过你\n"); end = 1; } int count = 0; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (chess[i][j] == 'x ' || chess[i][j] == 'o') { count++; } } } if (count == 0) { if (Pwin() != 1) { printf("菜B,居然和电脑平局\n"); end = 1; } } if (count == 0) { if (Cwin() != 1) { printf("菜B,连电脑都下不过\n"); end = 1; } } }int main() { init(); // 初始化棋盘 srand((unsigned)time(0)); while (1) { system("cls"); // 用于清屏,看起来舒服,haha table(); // 打印棋盘 PlayerInput(); // 玩家输入 ComputerInput(); // 电脑输入 IsWinner(); if (end == 1) { table(); break; } } system("pause"); return 0;}

 

转载地址:http://xqwzi.baihongyu.com/

你可能感兴趣的文章
Android开发——ListView的复用机制源码解析
查看>>
Android开发——WebView轻量缓存优化
查看>>
Android开发——Android7.0的适配注意点小结
查看>>
Android开发——架构组件LiveData源码解析
查看>>
IDEA常用快捷键整理
查看>>
【Vue】两个元素同一行显示
查看>>
XXL-Job使用
查看>>
如何在SwaggerAPI中添加统一授权认证
查看>>
多线程
查看>>
【Linux】Centos7 常用命令
查看>>
【Redis】Centos7下安装Redis
查看>>
【Redis】Centos7下搭建Redis集群
查看>>
【Redis】Centos7下搭建Redis集群——哨兵模式
查看>>
【Linux】本地ping不同VM虚拟机
查看>>
【SpringCloud】Hystrix
查看>>
乐观锁、悲观锁、公平锁、可重入锁
查看>>
快速阅读——《认知篇》
查看>>
一本书的学习
查看>>
如何画思维导图?
查看>>
数据库范式简单讲解(1NF、2NF、3NF、4NF、BCNF)
查看>>