
通过C++语言来实现一个以windows控制台为展示平台的简单版五子棋程序,其中通过键盘输入来控制游戏中的行为(光标移动、落子、确认)。
规则要求某一方在横竖斜方向连续存在五个或五个以上本人所执棋子获得为获胜。当我们要扒一个已存在的程序时(有的是五子棋的程序,可以在互联网里找到很多)。
我们可以从他的UI入手,通过我们所观察到的,所感受到,所使用到的服务,来对软件进行分析,从而获得以上流程,但我们一旦需要将需求变为代码时,我们的设计就要考虑的更多了。
我们可以使用两个int类型的值来表示:白子- 1,黑子- 2,那么我们只要在棋盘中更改光标所在位置元素的值为1或2就可以了。
我们回顾一下光标移动的代码,我们会发现,中进行落子后,我们光标再次移动有可能会改变已记录的落子信息,为了使光标与棋子不冲突,我们使用两个图层,表示两个相同的棋盘。
#include "iostream"#include <iomanip>
using namespace std
const int M=20
const int N=20
int main()
{
char weizhi[M][N]
int k,i,j,x,y,flag=0
cout<<"欢迎使用简易双人对战五子棋游戏"<<endl
cout<<"五子棋棋谱如下:"<<endl
for(k=0k<=Nk++)
cout<<setw(3)<<setfill(' ')<<k
cout<<endl
for(i=1i<=Mi++)
{
cout<<setw(3)<<setfill(' ')<<i
for(j=1j<=Nj++)
{
weizhi[i][j]='-'
cout<<setw(3)<<setfill(' ')<<weizhi[i][j]
}
cout<<endl
}
while(flag==0)
{
//红方落子
cout<<"请红方输入落子位置:"<<endl
loop1:
cout<<"请输入落子的行数:"
cin>>x
cout<<"请输入落子的列数:"
cin>>y
if(weizhi[x][y]=='-')
{
weizhi[x][y]='*'
for(k=0k<=Nk++)
cout<<setw(3)<<setfill(' ')<<k
cout<<endl
for(i=1i<=Mi++)
{
cout<<setw(3)<<setfill(' ')<<i
for(j=1j<=Nj++)
cout<<setw(3)<<setfill(' ')<<weizhi[i][j]
cout<<endl
}
}
else
{
cout<<"你不能在这落子,请重新选择落子位置:"<<endl
goto loop1
}
//判断胜利
for(i=1i<=M-4i++)
{
for(j=1j<=N-4j++)
{
if(weizhi[i][j]=='*' &&weizhi[i][j+1]=='*' &&weizhi[i][j+2]=='*' &&weizhi[i][j+3]=='*' &&weizhi[i][j+4]=='*')
{
cout<<"恭喜红方获得简易双人对战五子棋的胜利!耶~~~"<<endl
flag=1
break
}
if(weizhi[i][j]=='*' &&weizhi[i+1][j]=='*' &&weizhi[i+2][j]=='*' &&weizhi[i+3][j]=='*' &&weizhi[i+4][j]=='*')
{
cout<<"恭喜红方获得简易双人对战五子棋的胜利!耶~~~"<<endl
flag=1
break
}
if(weizhi[i][j]=='*' &&weizhi[i+1][j+1]=='*' &&weizhi[i+2][j+2]=='*' &&weizhi[i+3][j+3]=='*' &&weizhi[i+4][j+4]=='*')
{
cout<<"恭喜红方获得简易双人对战五子棋的胜利!耶~~~"<<endl
flag=1
break
}
if(flag==1)
break
}
}
//蓝方落子
cout<<"请蓝方输入落子位置:"<<endl
loop2:
cout<<"请输入落子的行数:"
cin>>x
cout<<"请输入落子的列数:"
cin>>y
if(weizhi[x][y]=='-')
{
weizhi[x][y]='#'
for(k=0k<=Nk++)
cout<<setw(3)<<setfill(' ')<<k
cout<<endl
for(i=1i<=Mi++)
{
cout<<setw(3)<<setfill(' ')<<i
for(j=1j<=Nj++)
cout<<setw(3)<<setfill(' ')<<weizhi[i][j]
cout<<endl
}
}
else
{
cout<<"你不能在这落子,请重新选择落子位置:"
goto loop2
}
//判断胜利
for(i=1i<=M-4i++)
{
for(j=1j<=N-4j++)
{
if(weizhi[i][j]=='#' &&weizhi[i][j+1]=='#' &&weizhi[i][j+2]=='#' &&weizhi[i][j+3]=='#' &&weizhi[i][j+4]=='#')
{
cout<<"恭喜蓝方获得简易双人对战五子棋的胜利!耶~~~"<<endl
flag=1
break
}
if(weizhi[i][j]=='#' &&weizhi[i+1][j]=='#' &&weizhi[i+2][j]=='#' &&weizhi[i+3][j]=='#' &&weizhi[i+4][j]=='#')
{
cout<<"恭喜蓝方获得简易双人对战五子棋的胜利!耶~~~"<<endl
flag=1
break
}
if(weizhi[i][j]=='#' &&weizhi[i+1][j+1]=='#' &&weizhi[i+2][j+2]=='#' &&weizhi[i+3][j+3]=='#' &&weizhi[i+4][j+4]=='#')
{
cout<<"恭喜蓝方获得简易双人对战五子棋的胜利!耶~~~"<<endl
flag=1
break
}
if(flag==1)
break
}
}
}
return 0
}
我运行过,没有错误.
package day17.gobangimport java.util.Arrays
public class GoBangGame {
public static final char BLANK='*'
public static final char BLACK='@'
public static final char WHITE='O'
public static final int MAX = 16
private static final int COUNT = 5
//棋盘
private char[][] board
public GoBangGame() {
}
//开始游戏
public void start() {
board = new char[MAX][MAX]
//把二维数组都填充‘*’
for(char[] ary: board){
Arrays.fill(ary, BLANK)
}
}
public char[][] getChessBoard(){
return board
}
public void addBlack(int x, int y) throws ChessExistException{
//@
//char blank = '*'
//System.out.println( x +"," + y + ":" + board[y][x] + "," + BLANK)
if(board[y][x] == BLANK){// x, y 位置上必须是空的才可以添棋子
board[y][x] = BLACK
return
}
throw new ChessExistException("已经有棋子了!")
}
public void addWhite(int x, int y)
throws ChessExistException{
if(board[y][x] == BLANK){// x, y 位置上必须是空的才可以添棋子
board[y][x] = WHITE
return
}
throw new ChessExistException("已经有棋子了!")
}
//chess 棋子:'@'/'O'
public boolean winOnY(char chess, int x, int y){
//先找到y方向第一个不是 blank的棋子
int top = y
while(true){
if(y==0 || board[y-1][x]!=chess){
//如果y已经是棋盘的边缘, 或者的前一个不是chess
//就不再继续查找了
break
}
y--
top = y
}
//向回统计所有chess的个数,如果是COUNT个就赢了
int count = 0
y = top
while(true){
if(y==MAX || board[y][x]!=chess){
//如果找到头 或者 下一个子不是chess 就不再继续统计了
break
}
count++
y++
}
return count==COUNT
}
//chess 棋子:'@'/'O'
public boolean winOnX(char chess, int x, int y){
//先找到x方向第一个不是 blank的棋子
int top = x
while(true){
if(x==0 || board[y][x-1]!=chess){
//如果x已经是棋盘的边缘, 或者的前一个不是chess
//就不再继续查找了
break
}
x--
top = x
}
//向回统计所有chess的个数,如果是COUNT个就赢了
int count = 0
x = top
while(true){
if(x==MAX || board[y][x]!=chess){
//如果找到头 或者 下一个子不是chess 就不再继续统计了
break
}
count++
x++
}
return count==COUNT
}
//chess 棋子:'@'/'O'
public boolean winOnXY(char chess, int x, int y){
//先找MAX向第一个不是 blank的棋子
int top = y
int left = x
while(true){
if(x==0 || y==0 || board[y-1][x-1]!=chess){
//如果x已经是棋盘的边缘, 或者的前一个不是chess
//就不再继续查找了
break
}
x--
y--
top = y
left=x
}
//向回统计所有chess的个数,如果是COUNT个就赢了
int count = 0
x = left
y = top
while(true){
if(x==MAX || y==MAX || board[y][x]!=chess){
//如果找到头 或者 下一个子不是chess 就不再继续统计了
break
}
count++
x++
y++
}
return count==COUNT
}
//chess 棋子:'@'/'O'
public boolean winOnYX(char chess, int x, int y){
//先找到x方向第一个不是 blank的棋子
int top = y
int left = x
while(true){
if(x==MAX-1 || y==0 || board[y-1][x+1]!=chess){
//如果x已经是棋盘的边缘, 或者的前一个不是chess
//就不再继续查找了
break
}
x++
y--
top = y
left=x
}
//向回统计所有chess的个数,如果是COUNT个就赢了
int count = 0
x = left
y = top
while(true){
if(x==0 || y==MAX || board[y][x]!=chess){
//如果找到头 或者 下一个子不是chess 就不再继续统计了
break
}
count++
x--
y++
}
return count==COUNT
}
public boolean whiteIsWin(int x, int y) {
//在任何一个方向上赢了,都算赢
return winOnY(WHITE, x, y) ||
winOnX(WHITE, x, y) ||
winOnXY(WHITE, x, y) ||
winOnYX(WHITE, x, y)
}
public boolean blackIsWin(int x, int y) {
return winOnY(BLACK, x, y) ||
winOnX(BLACK, x, y) ||
winOnXY(BLACK, x, y) ||
winOnYX(BLACK, x, y)
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)