Java 小程序

Java 小程序,第1张

我有个300多行的单机版五子棋。。不知道你说的小程序是指在网页上运行的,还是代码量少的程序。

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

import java.awt.*

import java.awt.event.*

import javax.swing.*

class mypanel extends Panel implements MouseListener

{

int chess[][] = new int[11][11]

boolean Is_Black_True

mypanel()

{

Is_Black_True = true

for(int i = 0i <11i++)

{

for(int j = 0j <11j++)

{

chess[i][j] = 0

}

}

addMouseListener(this)

setBackground(Color.BLUE)

setBounds(0, 0, 360, 360)

setVisible(true)

}

public void mousePressed(MouseEvent e)

{

int x = e.getX()

int y = e.getY()

if(x <25 || x >330 + 25 ||y <25 || y >330+25)

{

return

}

if(chess[x/30-1][y/30-1] != 0)

{

return

}

if(Is_Black_True == true)

{

chess[x/30-1][y/30-1] = 1

Is_Black_True = false

repaint()

Justisewiner()

return

}

if(Is_Black_True == false)

{

chess[x/30-1][y/30-1] = 2

Is_Black_True = true

repaint()

Justisewiner()

return

}

}

void Drawline(Graphics g)

{

for(int i = 30i <= 330i += 30)

{

for(int j = 30j <= 330j+= 30)

{

g.setColor(Color.WHITE)

g.drawLine(i, j, i, 330)

}

}

for(int j = 30j <= 330j += 30)

{

g.setColor(Color.WHITE)

g.drawLine(30, j, 330, j)

}

}

void Drawchess(Graphics g)

{

for(int i = 0i <11i++)

{

for(int j = 0j <11j++)

{

if(chess[i][j] == 1)

{

g.setColor(Color.BLACK)

g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16)

}

if(chess[i][j] == 2)

{

g.setColor(Color.WHITE)

g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16)

}

}

}

}

void Justisewiner()

{

int black_count = 0

int white_count = 0

int i = 0

for(i = 0i <11i++)//横向判断

{

for(int j = 0j <11j++)

{

if(chess[i][j] == 1)

{

black_count++

if(black_count == 5)

{

JOptionPane.showMessageDialog(this, "黑棋胜利")

Clear_Chess()

return

}

}

else

{

black_count = 0

}

if(chess[i][j] == 2)

{

white_count++

if(white_count == 5)

{

JOptionPane.showMessageDialog(this, "白棋胜利")

Clear_Chess()

return

}

}

else

{

white_count = 0

}

}

}

for(i = 0i <11i++)//竖向判断

{

for(int j = 0j <11j++)

{

if(chess[j][i] == 1)

{

black_count++

if(black_count == 5)

{

JOptionPane.showMessageDialog(this, "黑棋胜利")

Clear_Chess()

return

}

}

else

{

black_count = 0

}

if(chess[j][i] == 2)

{

white_count++

if(white_count == 5)

{

JOptionPane.showMessageDialog(this, "白棋胜利")

Clear_Chess()

return

}

}

else

{

white_count = 0

}

}

}

for(i = 0i <7i++)//左向右斜判断

{

for(int j = 0j <7j++)

{

for(int k = 0k <5k++)

{

if(chess[i + k][j + k] == 1)

{

black_count++

if(black_count == 5)

{

JOptionPane.showMessageDialog(this, "黑棋胜利")

Clear_Chess()

return

}

}

else

{

black_count = 0

}

if(chess[i + k][j + k] == 2)

{

white_count++

if(white_count == 5)

{

JOptionPane.showMessageDialog(this, "白棋胜利")

Clear_Chess()

return

}

}

else

{

white_count = 0

}

}

}

}

for(i = 4i <11i++)//右向左斜判断

{

for(int j = 6j >= 0j--)

{

for(int k = 0k <5k++)

{

if(chess[i - k][j + k] == 1)

{

black_count++

if(black_count == 5)

{

JOptionPane.showMessageDialog(this, "黑棋胜利")

Clear_Chess()

return

}

}

else

{

black_count = 0

}

if(chess[i - k][j + k] == 2)

{

white_count++

if(white_count == 5)

{

JOptionPane.showMessageDialog(this, "白棋胜利")

Clear_Chess()

return

}

}

else

{

white_count = 0

}

}

}

}

}

void Clear_Chess()

{

for(int i=0i<11i++)

{

for(int j=0j<11j++)

{

chess[i][j]=0

}

}

repaint()

}

public void paint(Graphics g)

{

Drawline(g)

Drawchess(g)

}

public void mouseExited(MouseEvent e){}

public void mouseEntered(MouseEvent e){}

public void mouseReleased(MouseEvent e){}

public void mouseClicked(MouseEvent e){}

}

class myframe extends Frame implements WindowListener

{

mypanel panel

myframe()

{

setLayout(null)

panel = new mypanel()

add(panel)

panel.setBounds(0,23, 360, 360)

setTitle("单人版五子棋")

setBounds(200, 200, 360, 383)

setVisible(true)

addWindowListener(this)

}

public void windowClosing(WindowEvent e)

{

System.exit(0)

}

public void windowDeactivated(WindowEvent e){}

public void windowActivated(WindowEvent e){}

public void windowOpened(WindowEvent e){}

public void windowClosed(WindowEvent e){}

public void windowIconified(WindowEvent e){}

public void windowDeiconified(WindowEvent e){}

}

public class mywindow

{

public static void main(String argc [])

{

myframe f = new myframe()

}

}

using System

using System.Collections.Generic

using System.ComponentModel

using System.Data

using System.Drawing

using System.Text

using System.Windows.Forms

using System.Collections

namespace RIF

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent()

}

public class Arr

{

public int[,] arr = new int[25, 25]

public bool win

}

Arr a = new Arr()

bool cc = true

public void PaintLab()

{

Bitmap image = new Bitmap(300,300)

Graphics g = Graphics.FromImage(image)

g.Clear(Color.Tan)

Pen pen = new Pen(Color.Black, 1)

int i, j

i = j = 0

while (i <= 300)

{

g.DrawLine(pen, i, 0, i, 300)

i = i + 20

}

while (j <= 300)

{

g.DrawLine(pen, 0, j, 300, j)

j = j + 20

}

img.Image = image

}

private void Form1_Load(object sender, EventArgs e)

{

PaintLab()

}

private void img_MouseClick(object sender, MouseEventArgs e)

{

if (e.Button == MouseButtons.Left)

{

int x = e.X

int y = e.Y

Graphics g = Graphics.FromImage(img.Image)

Brush pen

int myx = x / 20

int myy = y / 20

if (a.arr[myx, myy] != 0)

{

MessageBox.Show("这里已经有棋子了!")

return

}

else

{

if (cc)

{

pen = new SolidBrush(Color.White)

cc = false

a.arr[myx, myy] = 1

}

else

{

pen = new SolidBrush(Color.Black)

cc = true

a.arr[myx, myy] = 2

}

g.FillEllipse(pen, myx * 20 + 2, myy * 20 + 2, 16, 16)

img.Invalidate()

int z = IsWin(myx, myy, cc)

if (z != 0)

{

if (z == 1)

{

MessageBox.Show("白色获胜!")

}

else

{

MessageBox.Show("黑色获胜!")

}

img.Enabled = false

}

}

}

else

{

MessageBox.Show("本程序由Cantahu开发","作者信息",MessageBoxButtons.OK,MessageBoxIcon.Information)

}

}

private int IsWin(int x, int y,bool cc)

{

int m, n, count, p, q

int val = 0

bool win=false

if (cc)

{

val = 2

}

else

{

val = 1

}

#region 横向判断

count = 1

int f = 0

m = x-1

n = x+1

while (1==1)

{

if (count == 5)

{

win = true

break

}

else if (f == 5)

{

win = false

break

}

if (m >= 0 &&n <= 300)

{

if (a.arr[m, y] == val)

{

count = count + 1

m = m - 1

}

if (a.arr[n, y] == val)

{

count = count + 1

n = n + 1

}

}

f = f + 1

}

if (win)

{

return val

}

#endregion

#region 纵向判断

m = y - 1

n = y + 1

f = 0

count = 1

while (1 == 1)

{

if (count == 5)

{

win = true

break

}

if (f == 5)

{

win = false

break

}

if (m >= 0 &&n <= 300)

{

if(a.arr[x,m]==val)

{

count = count + 1

m = m - 1

}

if(a.arr[x,n]==val)

{

count = count + 1

n = n + 1

}

}

f = f + 1

}

if (win)

{

return val

}

#endregion

#region 左斜向判断

count = 1

f = 0

m = x - 1

n = y - 1

p = x + 1

q = y + 1

while (1 == 1)

{

if (count == 5)

{

win = true

break

}

if (f == 5)

{

win = false

break

}

if (m >= 0 &&n >= 0 &&p <= 300 &&q <= 300)

{

if (a.arr[m, n] == val)

{

count = count + 1

m = m - 1

n = n - 1

}

if (a.arr[p, q] == val)

{

count = count + 1

p = p + 1

q = q + 1

}

}

f = f + 1

}

if (win)

{

return val

}

#endregion

#region 右斜向

count = 1

f = 0

m = x - 1

n = y + 1

p = x + 1

q = y - 1

while (1 == 1)

{

if (count == 5)

{

win = true

break

}

if (f == 5)

{

win = false

break

}

if (m >= 0 &&n <= 300 &&p <= 300 &&q >= 0)

{

if (a.arr[m, n] == val)

{

count = count + 1

m = m - 1

n = n + 1

}

if (a.arr[p, q] == val)

{

count = count + 1

p = p + 1

q = q - 1

}

}

f = f + 1

}

if (win)

{

return val

}

#endregion

return 0

}

private void Btnstart_Click(object sender, EventArgs e)

{

img.Enabled = true

PaintLab()

}

private void btnClose_Click(object sender, EventArgs e)

{

this.Close()

}

}

}

这是我自己写的 五子棋代码 希望对你有帮助


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12101755.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-21
下一篇2023-05-21

发表评论

登录后才能评论

评论列表(0条)

    保存