C++ 编写X子棋代码

本文介绍了一款使用C++编写的X子棋游戏,玩家需要输入坐标来下棋。游戏规则类似五子棋,目标是连成X个同色棋子获胜。文章详细讲解了游戏思路、代码实现,包括输入处理、棋盘检查和胜利判断等功能。作者还提到后续会推出鼠标点击版的X子棋,以提升用户体验。
摘要由CSDN通过智能技术生成

最近都没更新了,Dev-c++也都发霉了。我只好拿出我上编程班的游戏作业:X子棋。

这次的X子棋是要输入坐标才能下棋的,跟我以往的数字华容道消消乐走迷宫(只需鼠标点击的)整体编程风格不太一样(主要是我没时间更新)。

如果觉得输入坐标太过麻烦,游戏体验感不太好,以后我也会带来更新(鼠标点击)版本的X子棋。

提示:本篇博客文字(代码除外)中N(rc)、X(n)以及X-1(n-1)表示 原版变量名N、X和改版变量名rc、n。均不表示乘法:N×rc、X×n以及(X-1)×(n-1)

规则

X子棋只不过是一个五子棋(也可以说是井字棋)的升级版,把"五"换成了"X"。

两个非计算机玩家参与对战,输入棋盘边长N(int rc)、胜利必须以横、竖、斜任意一种形式连成的同色子数目X(int n)以及先手('B'为黑,'W'为白)。

双方轮流下棋,任意一方先连成X(n)个同色子时胜利,或棋盘已满但双方均未胜利(宣布平局)。

思路

首先按上述方法通过输入获取rc、n和'B'或'W',确定此次棋局的初始基本条件。

可设定一个char型变量bw,用来确定这次下棋应轮到何方。

bw=='B'时,轮到黑方落子;相反bw=='W'时,轮到白方落子。

bw的初始值由上述的输入字符('B'或'W')确定,bw也可以直接当作被输入对象(需要作不合法处理)。

再接着构造一个do-while循环(for循环、while循环均可,要看个人编程习惯和喜好),用来表示棋局轮流循环。

do-while循环内部:

        一开始,先要给双方查看当前的棋局。我在棋局的行列开始处加了行列数字的提示,以防双方输入坐标时数错。如果觉得多此一举可以省去。

        接着获取当前落子方(由bw决定)的落子坐标(x,y)。

                不合法处理:

   

下面是一个简单的井字棋小游戏的 C++ 代码,希望能帮到你: ```c++ #include <iostream> using namespace std; char square[10] = {'0','1','2','3','4','5','6','7','8','9'}; int checkwin(); void board(); int main() { int player = 1,i,choice; char mark; do { board(); player=(player%2)?1:2; cout << "Player " << player << ", enter a number: "; cin >> choice; mark=(player == 1) ? 'X' : 'O'; if (choice == 1 && square[1] == '1') square[1] = mark; else if (choice == 2 && square[2] == '2') square[2] = mark; else if (choice == 3 && square[3] == '3') square[3] = mark; else if (choice == 4 && square[4] == '4') square[4] = mark; else if (choice == 5 && square[5] == '5') square[5] = mark; else if (choice == 6 && square[6] == '6') square[6] = mark; else if (choice == 7 && square[7] == '7') square[7] = mark; else if (choice == 8 && square[8] == '8') square[8] = mark; else if (choice == 9 && square[9] == '9') square[9] = mark; else { cout<<"Invalid move "; player--; cin.ignore(); cin.get(); } i=checkwin(); player++; }while(i==-1); board(); if(i==1) cout<<"==>\aPlayer "<<--player<<" win "; else cout<<"==>\aGame draw"; cin.ignore(); cin.get(); return 0; } int checkwin() { if (square[1] == square[2] && square[2] == square[3]) return 1; else if (square[4] == square[5] && square[5] == square[6]) return 1; else if (square[7] == square[8] && square[8] == square[9]) return 1; else if (square[1] == square[4] && square[4] == square[7]) return 1; else if (square[2] == square[5] && square[5] == square[8]) return 1; else if (square[3] == square[6] && square[6] == square[9]) return 1; else if (square[1] == square[5] && square[5] == square[9]) return 1; else if (square[3] == square[5] && square[5] == square[7]) return 1; else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4' && square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8' && square[9] != '9') return 0; else return -1; } void board() { system("cls"); cout << "\n\n\tTic Tac Toe\n\n"; cout << "Player 1 (X) - Player 2 (O)" << endl << endl; cout << endl; cout << " | | " << endl; cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl; cout << "_____|_____|_____" << endl; cout << " | | " << endl; cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl; cout << "_____|_____|_____" << endl; cout << " | | " << endl; cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl; cout << " | | " << endl << endl; } ``` 这个代码中,我们使用了一个字符数组来存储井字棋的状态,每次玩家输入一个数字代表着玩家在棋盘上落子,程序会判断输入是否合法,如果合法,将玩家指定的字符存储到字符数组中。每次落子后,程序调用 checkwin() 函数来判断是否有玩家胜利或平局,如果有,游戏结束,输出结果。如果没有,继续进行下一轮游戏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值