简单的五子棋

[size=large] [color=darkred]五子棋的开发[/color] [/size]

一.五子棋开发的难点
1.算法
具体是五子棋排放,判断输赢的方法。
2.代码结构复杂度提高

二.技术点:
1.JFrame, Graphics, 按钮
2.事件
3.重绘
4.算法

其实学习五子棋是为了复习巩固以前学习的一些内容。掌握JFrame的基本操作,过去画板,添加按钮,在按钮这个事件上添加监听器。掌握继承的方法。实现重绘,能够展现一个简单的五子棋对战的小游戏。最后能够实现人机对战。编写方法,用算法实现这个。掌握五子棋的具体算法。

三.过程分解
1.在窗体的画板上,画上直线与横线形成一棋盘格子(建立坐标)
应用坐标的关系,画出五子棋的线条布局,实现一个五子棋的基本框架。

2.在窗体的画板上绘制棋子(事件处理)
给事件添加接口,实现接口,能够绘制五子棋,黑白相间的。

3.人人对战完成胜负判断(下一颗白子接着一颗黑子)
写出算法,能够判断输赢,实现自己和自己下棋。

4.人机对战(机器人下棋策略,也就是五子算法)


[/code][code="java"]package cn.hsm.java;


import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseListener;

import javax.swing.JFrame;

public class FiveChessTest extends JFrame
{
public static void main(String[] args)
{
FiveChessTest fc = new FiveChessTest();
fc.showUI();
}

private int[][] location = new int[Name.HEIGHT][Name.WIDTH];
private Graphics g;

public void showUI() {
this.setTitle("hsm五子棋···");
this.setSize(700, 700);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.setVisible(true);

Graphics g = getGraphics();
MouseListenerImpl mouseListener = new MouseListenerImpl(g ,location);
this.addMouseListener(mouseListener);
}
/*
* 重写paint函数
*/
public void paint(Graphics g)
{
super.paint(g);
//画棋盘的横线
for(int i = 0; i

-----------------------------------------------------------------


package cn.hsm.java;

import java.awt.Color;
import java.awt.Graphics;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MouseListenerImpl extends MouseAdapter{
private Graphics g;
private int x1,y1;
private int choose = 1;
private int[][] location;
public MouseListenerImpl(Graphics g ,int[][] location)
{
this.g =g;
this.location = location;
}



public void mouseReleased(MouseEvent e)
{
//鼠标点击的位置
x1 = e.getX();
y1 = e.getY();
int x0; //棋盘交叉点的x坐标
int y0; //棋盘交叉点的y坐标
for (int a = 0; a < Name.WIDTH; a++)
{
for (int b = 0; b < Name.HEIGHT; b++)
{
x0 = Name.X0 + a * Name.CELL_SIZE;
y0 = Name.Y0 + b * Name.CELL_SIZE;
if((Math.abs(x1 - x0) < Name.CELL_SIZE / 2) && (Math.abs(y1 - y0) < Name.CELL_SIZE / 2) &&location[a][b] == 0) {


//画出棋子,且分出颜色:1为黑色,-1为白色;判断当前画的棋子颜色,并将其存储到二维数组中
if(choose == 1)
{
g.setColor(Color.BLACK);
g.fillOval(x0 - Name.CHESS_SIZE/2, y0 - Name.CHESS_SIZE/2, Name.CHESS_SIZE, Name.CHESS_SIZE);
location[a][b] = 1;
win01Ways(a, b, choose);
win02Ways(a, b, choose);
win03Ways(a, b, choose);
choose = -1;
System.out.println("black");
return;
}
if (choose == -1)
{
g.setColor(Color.WHITE);
g.fillOval(x0 - Name.CHESS_SIZE/2, y0 - Name.CHESS_SIZE/2, Name.CHESS_SIZE, Name.CHESS_SIZE);
location[a][b] = -1;
win01Ways(a, b, choose);
win02Ways(a, b, choose);
win03Ways(a, b, choose);
choose = 1 ;
System.out.println("white");
}
}
}
}
}


private void win01Ways(int x, int y, int choose) {
int count = 1;
//判断横向是否连珠
if(choose == 1) {
for(int i = 1; i < 5; i++) {
if(location[x-i][y] == 1) {
count++;
}else{
break;
}
}
for(int j = 1; j < 5; j++) {
if(location[x+j][y] == 1) {
count++;
}else{
break;
}
}
}else {
for(int i = 1; i < 5; i++) {
if(location[x-i][y] == -1) {
count++;
}else{
break;
}
}
for(int j = 1; j < 5; j++) {
if(location[x+j][y] == -1) {
count++;
}else{
break;
}
}
}
if (count > 4)
{
System.out.println("有人获胜啦!!");
}
}

private void win02Ways(int x, int y, int choose) {
int count = 1;
//判断纵向是否连珠
if(choose == 1) {
for(int i = 1; i < 5; i++) {
if(location[x][y-i] == 1) {
count++;
}else{
break;
}
}
for(int j = 1; j < 5; j++) {
if(location[x][y+j] == 1) {
count++;
}else{
break;
}
}
}else {
for(int i = 1; i < 5; i++) {
if(location[x][y-i] == -1) {
count++;
}else{
break;
}
}
for(int j = 1; j < 5; j++) {
if(location[x][y+j] == -1) {
count++;
}else{
break;
}
}
}
if (count > 4)
{
System.out.println("有人获胜啦!!");
}
}

private void win03Ways(int x, int y, int choose) {
int count = 1;
//判断纵向是否连珠
if(choose == 1) {
for(int i = 1; i < 5; i++) {
if(location[x-i][y-i] == 1) {
count++;
}else{
break;
}
}
for(int j = 1; j < 5; j++) {
if(location[x+j][y+j] == 1) {
count++;
}else{
break;
}
}
}else {
for(int i = 1; i < 5; i++) {
if(location[x-i][y-i] == -1) {
count++;
}else{
break;
}
}
for(int j = 1; j < 5; j++) {
if(location[x+j][y+j] == -1) {
count++;
}else{
break;
}
}
}
if (count > 4)
{
System.out.println("有人获胜啦!!");
}
}


}

------------------------------------------------------------------



package cn.hsm.java;

public class Name {
public static int X0 = 50; //横坐标的偏移量
public static int Y0 = 50; //纵坐标的偏移量
public static int CELL_SIZE= 50; // 方格的大小
public static int HEIGHT = 12; //高的数目
public static int WIDTH = 12; //宽的数目
public static int CHESS_SIZE = 40; //棋子的大小
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单五子棋游戏,代码仅100行左右: ```python import numpy as np # 初始化棋盘 def create_board(): board = np.zeros((15, 15)) return board # 判断是否胜利 def win_move(board, player): # 横向 for i in range(15): for j in range(11): if board[i][j] == player and board[i][j+1] == player and board[i][j+2] == player and board[i][j+3] == player and board[i][j+4] == player: return True # 纵向 for i in range(11): for j in range(15): if board[i][j] == player and board[i+1][j] == player and board[i+2][j] == player and board[i+3][j] == player and board[i+4][j] == player: return True # 斜向 for i in range(11): for j in range(11): if board[i][j] == player and board[i+1][j+1] == player and board[i+2][j+2] == player and board[i+3][j+3] == player and board[i+4][j+4] == player: return True for i in range(11): for j in range(4, 15): if board[i][j] == player and board[i+1][j-1] == player and board[i+2][j-2] == player and board[i+3][j-3] == player and board[i+4][j-4] == player: return True return False # 定义主函数 def game(): board = create_board() # 玩家1用1表示,玩家2用2表示 player = 1 while True: print(board) if player == 1: row = int(input("玩家1,请输入落子的行号:")) col = int(input("玩家1,请输入落子的列号:")) board[row][col] = 1 if win_move(board, player): print("玩家1获胜!") break player = 2 else: row = int(input("玩家2,请输入落子的行号:")) col = int(input("玩家2,请输入落子的列号:")) board[row][col] = 2 if win_move(board, player): print("玩家2获胜!") break player = 1 game() ``` 运行后,会提示玩家输入行号和列号,然后在棋盘上落子。当有一方胜利时,游戏结束并打印胜利方。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值