五子棋源代码输坐标(java简单五子棋源代码)
admin 发布:2022-12-19 21:21 166
今天给各位分享五子棋源代码输坐标的知识,其中也会对java简单五子棋源代码进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
c语言五子棋代码,
package day17.gobang;
import 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);
}
}
找五子棋源代码c++
#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=0;k=N;k++)
coutsetw(3)setfill(' ')k;
coutendl;
for(i=1;i=M;i++)
{
coutsetw(3)setfill(' ')i;
for(j=1;j=N;j++)
{
weizhi[i][j]='-';
coutsetw(3)setfill(' ')weizhi[i][j];
}
coutendl;
}
while(flag==0)
{
//红方落子
cout"请红方输入落子位置:"endl;
loop1:
cout"请输入落子的行数:";
cinx;
cout"请输入落子的列数:";
ciny;
if(weizhi[x][y]=='-')
{
weizhi[x][y]='*';
for(k=0;k=N;k++)
coutsetw(3)setfill(' ')k;
coutendl;
for(i=1;i=M;i++)
{
coutsetw(3)setfill(' ')i;
for(j=1;j=N;j++)
coutsetw(3)setfill(' ')weizhi[i][j];
coutendl;
}
}
else
{
cout"你不能在这落子,请重新选择落子位置:"endl;
goto loop1;
}
//判断胜利
for(i=1;i=M-4;i++)
{
for(j=1;j=N-4;j++)
{
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"请输入落子的行数:";
cinx;
cout"请输入落子的列数:";
ciny;
if(weizhi[x][y]=='-')
{
weizhi[x][y]='#';
for(k=0;k=N;k++)
coutsetw(3)setfill(' ')k;
coutendl;
for(i=1;i=M;i++)
{
coutsetw(3)setfill(' ')i;
for(j=1;j=N;j++)
coutsetw(3)setfill(' ')weizhi[i][j];
coutendl;
}
}
else
{
cout"你不能在这落子,请重新选择落子位置:";
goto loop2;
}
//判断胜利
for(i=1;i=M-4;i++)
{
for(j=1;j=N-4;j++)
{
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;
}
我运行过,没有错误.
java 五子棋的源代码 以及类图 用例图 时序图
import java.io.*;
class goboard
{
public String[][] board;
private static int BOADSIZE=15;
public void initBoard()
{
board=new String[BOADSIZE][BOADSIZE];
for(int i=0;iBOADSIZE;i++)
{
for(int j=0;jBOADSIZE;j++)
{
board[i][j]="+";
}
}
}
public void printboard()
{
for(int i=0;iBOADSIZE;i++)
{
for(int j=0;jBOADSIZE;j++)
{
System.out.print(board[i][j]);
}
System.out.println();
}
}
}
public class Wuziqi {
public static void main(String[] args) throws Exception
{
goboard gb=new goboard();
gb.initBoard();
gb.printboard();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String inputstr=null;
while((inputstr=br.readLine())!=null)
{
String[] posStrArr=inputstr.split(",");
int xpos=Integer.parseInt(posStrArr[0]);
int ypos=Integer.parseInt(posStrArr[1]);
gb.board[xpos-1][ypos-1]="●";
gb.printboard();
System.out.println("请输入您下棋的坐标,应为X,Y的格式:");
}
}
}
C语言—五子棋求大神帮忙看看,设计是按输入的坐标打印棋子。可打不出来
几个问题:
weizhi这个存放坐标的数组需要用二维数组,否则只能存放一行位置;
初始化棋盘的动作从draw函数里移出来,放在main函数里做,draw只负责把当前棋盘和棋子画出来,初始化棋盘就是输入棋盘的大小;
存放棋子的二维数组p需要初始化;
draw画棋盘的时候,要判断p数组对应存放了什么字符来决定输出什么内容;
判断棋子超过棋盘范围,只需要检查输入的坐标x和y是否都比棋盘小就可以了,放在for循环里,每次都会被执行到。
代码改了一下,你看一看。
#includestdio.h
#define N 20
#define X 10
struct zuobiao
{
int x;
int y;
};
struct zuobiao weizhi[N][N];
char p[N][N];
int len;
void draw ();
void main()
{
int i,j;
int a,b;
memset(p, 0, N*N);
printf ("Please input the five_chess's board\n");
scanf ("%d", len);
draw();
printf ("请输入一个坐标:例如 3,4 \n");
scanf("%d,%d", a,b);
if(a = len || b = len)
printf("棋子超过棋盘范围\n");
for(i=0;ilen;i++)
{
for(j=0;jlen;j++)
{
if ((weizhi[i][j].x==a) (weizhi[i][j].y==b))
{
p[a][b]='@';
draw();
}
}
}
}
void draw()
{
int i,j,k;
for (i = 0; ilen; i++)
{
if(i==0)
printf("%3d",i);
else
printf("%2d", i );
}
printf("\n");
for (i = 0; ilen; i++)
{
printf ("%2d", i );
for (k = 0; k len; k++)
{
if(p[i][k] == 0)
printf ("十");
else
printf ("%c ", p[i][k]);
weizhi[i][k].x=i;
weizhi[i][k].y=k;
}
printf ("\n");
}
}
五子棋源代码输坐标的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java简单五子棋源代码、五子棋源代码输坐标的信息别忘了在本站进行查找喔。
版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;
相关推荐
- 05-01购物网站asp.net源代码(购物网站jsp完整源码)[20240501更新]
- 05-01wordpress运行代码(wordpress 源代码)[20240501更新]
- 05-01html5留言板源代码(java留言板代码)[20240501更新]
- 04-30火锅店网站源代码(火锅店网页)[20240430更新]
- 04-30棋牌游戏代码的简单介绍[20240430更新]
- 04-30关于数据录入查询系统asp源代码的信息[20240430更新]
- 04-30vb解密代码(vb源代码加密)[20240430更新]
- 04-30看matlab函数代码(怎么看matlab函数的源代码)[20240430更新]
- 04-30网页源代码在线翻译(html网页源码翻译工具)[20240430更新]
- 04-30企业生产管理系统源代码(企业管理的代码)[20240430更新]
取消回复欢迎 你 发表评论:
- 标签列表
- 最近发表
- 友情链接