新手java五子棋完整代码判断落子落在线上_java五子棋小游戏实验报告代码

本文介绍了一种绘制棋盘及棋子的方法,通过定义格子的位置和尺寸,实现了棋盘的绘制,并在指定位置放置棋子。文章详细展示了如何使用图形绘制函数来创建棋盘的网格线以及棋子。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

idWidth, y + gridHeight, gridWidth, gridHeight);

if (width &gt height) {

mydrawRect(x + gridWidth * 2, y, gridWidth, gridHeight);

mydrawRect(x + gridWidth * 2, y + gridHeight, gridWidth, gridHeight);

//最右边的竖线

DrawChessman(x + gridWidth * 3, y);

DrawChessman(x + gridWidth * 3, y + gridHeight);

DrawChessman(x + gridWidth * 3, y + gridHeight * 2);

//最下面的横线

DrawChessman(x, y + gridHeight * 2);

DrawChessman(x + gridWidth, y + gridHeight * 2);

DrawChessman(x + gridWidth * 2, y + gridHeight * 2);

}

else {

mydrawRect(x, y + gridHeight * 2, gridWidth, gridHeight);

mydrawRect(x + gridWidth, y + gridHeight * 2, gridWidth, gridHeight);

//最右边的竖线

DrawChessman(x + gridWidth * 2, y);

DrawChessman(x + gridWidth * 2, y + gridHeight);

DrawChessman(x + gridWidth * 2, y + gridHeight * 2);

DrawChessman(x + gridWidth * 2, y + gridHeight * 3);

//最下面的横线

DrawChessman(x, y + gridHeight * 3);

DrawChessman(x + gridWidth, y + gridHeight * 3);

}

//画光标

gg.setColor(focusColor);

gg.drawLine(currentX, currentY,currentX + gridWidth /5, currentY);

gg.drawLine(currentX, currentY, currentX,currentY + gridHeight /5);

gg.drawLine(currentX +gridWidth*4/5,currentY,currentX + gridWidth, currentY);

gg.drawLine(currentX + gridWidth, currentY, currentX + gridWidth,currentY + gridHeight /5);

gg.drawLine(currentX,currentY + gridHeight * 4/ 5,currentX, currentY + gridHeight);

gg.drawLine(currentX,currentY + gridHeight,currentX + gridWidth / 5,currentY + gridHeight);

gg.drawLine(currentX +gridWidth*4/5,currentY + gridHeight, currentX + gridWidth,currentY + gridHeight);

gg.drawLine(currentX + gridWidth,currentY + gridHeight *4 / 5,currentX + gridWidth, currentY + gridHeight);

repaint();

}

catch (Exception e) {

System.out.println("moveFoucs Error:" + e);

}

}

/*

* 画格子,并画棋子。把这部分独立出来主要是要检查边界问题

* x格子右上角x

* y格子右上角y

* width格子宽度

* height格子高度

*/

private void mydrawRect(int x, int y, int width, int height) {

int a, b;

a = (x - baseX) / gridWidth + 1;

b = (y - baseY) / gridHeight + 1;

if (a != lineNumber && b != lineNumber) {

gg.setColor(lineColor);

gg.drawRect(x, y + 1, width, height);

}

DrawChessman(x, y);

}

//画棋子,x,y分别是圆的中心坐标,棋子的直径为格子边长(格子为正方形)的4/5

public void DrawChessman(int x, int y) {

try {

int a;

int b;

a = (x - baseX) / gridWidth + 1;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值