五子棋游戏(简单易懂,入门都能学)
10x10棋盘的五子棋游戏(玩家用“X”表示,电脑用“O”表示)感兴趣的可以试试,别看代码太长,其实代码很简单的,真的入门就能学!!!
头文件
#pragma once#include<iostream>using namespace std;#include<string>#include<cstdlib>#include<ctime>#define ROW 10#define COL 10//10x10棋盘void print();//打印是否进行游戏void bulidboard();//建立棋盘void display();//打印棋盘void player();//玩家下void computer();//电脑下int judgecontinue();//判断是棋盘有没有占满,进而判断是否能继续char judgewin();//判断输赢void game();//运行游戏函数文件
#include"五子棋游戏.h"#include<ctime>int r = ROW * COL;void print(){ cout << "*********<<五子棋游戏>>******" << endl; cout << "**********1.进入游戏*********" << endl; cout << "**********2.退出游戏*********" << endl; cout << "*****************************" << endl;}string board;//建立棋盘void bulidboard(){ for (int i = 0; i < ROW; i++) { for (int k = 0; k < COL; k++) { board=" ";//一个空格太小,设定为三个空格 } }}void display()//打印棋盘,并建立划分线{ for (int i = 0; i < ROW; i++) { for (int k = 0; k <COL; k++) { cout << board; if (k < 10 - 1) { cout << "|"; } } cout << endl; if (i < ROW - 1) { for (int k = 0; k < 10; k++) { cout << "---"; if (k <COL - 1) { cout << "|"; } } cout << endl; } }}void player(){ cout << "请输入下的位置(先输入行数后输入列数):"; int x, y;//x为行数,y为列数 int temp = 0; do { cin >> x >> y; if (x <= 10 && x >= 1 && y <= 10 && y >= 1) { if (board == " ") { board = " X ";//玩家用X表示 temp++; } else { cout << "位置已被占用,请重新输入!" << endl; } } else { cout << "位置越界,请重新输入!" << endl; } } while (temp == 0);}void computer(){ int a = 0; int x, y; srand(time(NULL)); do { x = rand() % ROW; y = rand() % COL; if (board == " ") { board = " O ";//电脑用O表示 a++; } } while (a == 0);}char judgewin()//判断输赢{ //判断行 for (int i = 0; i < ROW; i++)//玩家赢 { for (int k = 0; k < COL-4; k++) { if (board == board && board == board && board == board && board == board&&board!=" ") { if (board == " X ")//判断是否玩家赢 { return 'X';//玩家赢 } return 'O';//电脑赢 } } } //判断列 for (int k = 0; k < ROW; k++) { for (int i = 0; i < COL - 4; i++) { if (board == board && board == board && board == board && board == board && board!= " ") { if (board == " X ") { return 'X'; } return 'O'; } } }//判断斜线 //右斜线 for (int i = 0; i < 6; i++) { for (int k = 0; k < 6; k++) { if (board == board && board == board && board == board && board == board && board!= " ") { if (board == " X ") { return 'X'; } return 'O'; } } } //左斜线 for (int i = 9; i > 3; i--) { for (int k = 0; k < 6; k++) { if (board == board && board == board && board == board && board == board && board != " ") { if (board == " X ") { return 'X'; } return 'O'; } } } if (judgecontinue()) { return 'Q';//代表平局 } return 'C';//代表继续}int judgecontinue(){ for (int i = 0; i < ROW; i++) { for (int k = 0; k < COL; k++) { if (board != " ") { r--; } } } if (r == 0) { return 1; } return 0;}char temp;void game(){ int r = ROW * COL; bulidboard(); display(); while (true) { cout << "玩家下:" << endl; player(); display(); judgecontinue(); judgewin(); temp = judgewin(); if (temp != 'C') { break; } cout << endl; cout << "电脑下:"<<endl; computer(); display(); judgewin(); temp = judgewin(); cout << endl; if (temp != 'C') { break; } } if (temp == 'X') { cout << "恭喜你赢了" << endl; cout << "1.再玩一次" << endl; cout << "2.退出游戏" << endl; cout << "请输入数字进行操作:" << endl; } if (temp == 'O') { cout << "电脑赢" << endl; cout << "1.再玩一次" << endl; cout << "2.退出游戏" << endl; cout << "请输入数字进行操作:" << endl; } if (temp == 'Q') { cout << "平局" << endl; cout << "1.再玩一次" << endl; cout << "2.退出游戏" << endl; cout << "请输入数字进行操作:" << endl; }}主文件
#include<iostream>using namespace std;#include<string>#include"五子棋游戏.h"#include<windows.h>#include<ctime>#define ROW 10#define COL 10//10x10棋盘int main(){print();bulidboard();while (true){ int select; cin >> select; if (select <= 10000 && select >= 0) { if (select == 1 || select == 2) { switch (select) { case 1://如果代码过长,需要加花括号“{}” { cout << "正在进入游戏........" << endl; Sleep(1000); system("cls"); game(); break; } case 2: cout << "欢迎下次使用!" << endl; return 0; break; } } else { cout << "输入错误,请重新输入!" << endl; } } else { cout << "输入错误,请退出重试!!!" << endl; return 0; }}return 0;}效果
由于电脑太笨了,楼主实在弄不出平局的效果
楼主:我太难了!
https://blog.51cto.com/u_15403035/4609643
页:
[1]