最近都没更新了,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)。
不合法处理: