JAva五子棋ゲーム
13970 ワード
FiveChessMain.java
FiveChessFrame.java
package org.myFiveChess;
public class FiveChessMain {
public static void main(String args[]){
FiveChessFrame n = new FiveChessFrame();
}
}
FiveChessFrame.java
package org.myFiveChess;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class FiveChessFrame extends JFrame implements MouseListener,Runnable {
BufferedImage imageBack = null;
BufferedImage imageStart = null;
BufferedImage imageSet = null;
BufferedImage imageIntro = null;
BufferedImage imageGiveup = null;
BufferedImage imageAbout = null;
BufferedImage imageEnd = null;
Font font = new Font(" ", Font.CENTER_BASELINE, 25);
int x = 0, y = 0;
int[][] allChess = new int[19][19];
boolean isBlack = true;
boolean canPlay = true;
String message = " ";
String blackMessage = " ";
String whiteMessage = " ";
//
int blackTime = 0;
//
int whiteTime = 0;
//
int maxTime = 0;
//
Thread t = new Thread(this);
public FiveChessFrame() {
this.setTitle(" "); //
this.setSize(500, 500); //
int w = Toolkit.getDefaultToolkit().getScreenSize().width;
int h = Toolkit.getDefaultToolkit().getScreenSize().height;
String strBack = "C:/Users/guoximing/Pictures/five2.jpg";
String strStart = "C:/Users/guoximing/Pictures/startGame.jpg";
String strSet = "C:/Users/guoximing/Pictures/setGame.jpg";
String strIntro = "C:/Users/guoximing/Pictures/introGame.jpg";
String strGiveup = "C:/Users/guoximing/Pictures/giveupGame.jpg";
String strAbout = "C:/Users/guoximing/Pictures/aboutGame.jpg";
String strEnd = "C:/Users/guoximing/Pictures/end.jpg";
try {
imageBack = ImageIO.read(new File(strBack));
imageStart = ImageIO.read(new File(strStart));
imageSet = ImageIO.read(new File(strSet));
imageIntro = ImageIO.read(new File(strIntro));
imageGiveup = ImageIO.read(new File(strGiveup));
imageAbout = ImageIO.read(new File(strAbout));
imageEnd = ImageIO.read(new File(strEnd));
} catch (IOException e) {
System.out.println(" ");
e.printStackTrace();
}
this.setLocation((w - 500) / 2, (h - 500) / 2); //
this.setResizable(false); //
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //
this.addMouseListener(this);
this.setVisible(true); //
}
public void paint(Graphics g) {
BufferedImage bi = new BufferedImage(500, 500,
BufferedImage.TYPE_INT_ARGB);
Graphics g2 = bi.createGraphics();
g2.drawImage(imageBack, 0, 17, 500, 481, this);
// g.setColor(Color.GRAY);
// g.fillRect(0, 0, 500, 500);
g2.setColor(Color.GREEN);
g2.setFont(font);
g2.drawString(" :" + message, 10, 70);//
g2.setFont(new Font(" ", Font.CENTER_BASELINE, 25));
g2.setColor(Color.GREEN);
g2.drawString(" ", 10, 490);
g2.drawString(" ", 250, 490);
g2.setColor(Color.RED);
g2.setFont(new Font(" ", Font.CENTER_BASELINE, 20));
g2.drawString(blackMessage,120,490);
g2.drawString(whiteMessage, 380, 490);
g2.drawImage(imageStart, 380, 90, 110, 40, this);
g2.drawImage(imageSet, 380, 140, 110, 40, this);
g2.drawImage(imageIntro, 380, 190, 110, 40, this);
g2.drawImage(imageGiveup, 380, 310, 110, 40, this);
g2.drawImage(imageAbout, 380, 360, 110, 40, this);
g2.drawImage(imageEnd, 380, 410, 110, 40, this);
g2.setColor(Color.GRAY);
g2.fillRect(10, 90, 360, 360);
g2.setColor(Color.BLACK);
for (int i = 0; i <= 18; i++)
g2.drawLine(10, i * 20 + 90, 370, i * 20 + 90);
g2.setColor(Color.BLACK);
for (int i = 0; i <= 18; i++)
g2.drawLine(i * 20 + 10, 90, i * 20 + 10, 450);
for (int i = 0; i < 19; i++)
for (int j = 0; j < 19; j++) {
if (allChess[i][j] == 1) //
{
// g.setColor(Color.BLACK);
int tempx = i * 20 + 10;
int tempy = j * 20 + 90;
g2.fillOval(tempx - 7, tempy - 7, 14, 14);
} else if (allChess[i][j] == 2) //
{
g2.setColor(Color.WHITE);
int tempx = i * 20 + 10;
int tempy = j * 20 + 90;
g2.fillOval(tempx - 7, tempy - 7, 14, 14);
g2.setColor(Color.BLACK);
g2.drawOval(tempx - 7, tempy - 7, 14, 14);
}
}
g2.setColor(Color.GREEN);
g2.fillOval(68, 148, 4, 4);
g2.fillOval(188, 148, 4, 4);
g2.fillOval(308, 148, 4, 4);
g2.fillOval(68, 268, 4, 4);
g2.fillOval(188, 268, 4, 4);
g2.fillOval(308, 268, 4, 4);
g2.fillOval(68, 388, 4, 4);
g2.fillOval(188, 388, 4, 4);
g2.fillOval(308, 388, 4, 4);
g2.fillOval(x - 5, y - 5, 10, 10);
g.drawImage(bi, 0, 0, this);
}
@Override
public void mouseClicked(MouseEvent e) {
// int x = e.getX();
// int y = e.getY();
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
if (canPlay == true) {
if (x >= 10 && x <= 371 && y >= 90 && y <= 450) {
// System.out.println(" "+ x +" "+ y);
// if((x-10)%20==0&&(y-90)%20==0){
// x = (x - 10) / 20;
// y = (y - 90) / 20;
int xx = (x - 10) / 20;
int yy = (y - 90) / 20;
if ((x - 10) % 20 >= 10)
xx++;
if ((y - 90) % 20 >= 10)
yy++;
x = xx;
y = yy;
if (allChess[x][y] == 0) {
if (isBlack == true) {
allChess[x][y] = 1;
isBlack = false;
message = " ";
}
else {
allChess[x][y] = 2;
isBlack = true;
message = " ";
}
//
if (checkWin()) {
JOptionPane.showMessageDialog(this, " ,"
+ (allChess[x][y] == 1 ? " " : " ") + " !");
canPlay = false;
t.stop();
}
} else {
JOptionPane.showMessageDialog(this, " !");
}
// isBlack = !isBlack;
this.repaint();
}
}
if (x >= 380 && x <= 490 && y >= 90 && y <= 130)
//JOptionPane.showMessageDialog(this, " ");
{
int res = JOptionPane.showConfirmDialog(this," ?");
if(res == 0){
for(int i=0;i<=18;i++)
for(int j=0;j<=18;j++)
allChess[i][j] = 0;//allChess = new int[19][19];
canPlay = true;
isBlack = true;
message = " ";
blackTime = maxTime*60;
whiteTime = maxTime*60;
blackMessage = blackTime/3600 + ":" + (blackTime%3600)/60 + ":" + (blackTime%3600)%60;
whiteMessage = whiteTime/3600 + ":" + (whiteTime%3600)/60 + ":" + (whiteTime%3600)%60;
t.start();
this.repaint();
}
}
if (x >= 380 && x <= 490 && y >= 140 && y <= 180)
//JOptionPane.showMessageDialog(this, " ");
{
String input = JOptionPane.showInputDialog(this," ( ), 0, ");
try
{
maxTime = Integer.parseInt(input);
if(maxTime<0)
{
JOptionPane.showMessageDialog(this," ");
}
if(maxTime == 0)
{
int res = JOptionPane.showConfirmDialog(this," ( ), !");
if(res == 0){
for(int i=0;i<=18;i++)
for(int j=0;j<=18;j++)
allChess[i][j] = 0;//allChess = new int[19][19];
isBlack = true;
message = " ";
canPlay = true;
//blackTime = maxTime*60;
//whiteTime = maxTime*60;
//blackMessage = blackTime/3600 + ":" + (blackTime%3600)/60 + ":" + (blackTime%3600)%60;
//whiteMessage = whiteTime/3600 + ":" + (whiteTime%3600)/60 + ":" + (whiteTime%3600)%60;
//this.repaint();
}
}
if(maxTime >0)
{
int res1 = JOptionPane.showConfirmDialog(this," , !");
if(res1 == 0){
for(int i=0;i<=18;i++)
for(int j=0;j<=18;j++)
allChess[i][j] = 0;//allChess = new int[19][19];
isBlack = true;
canPlay = true;
message = " ";
blackTime = maxTime*60;
whiteTime = maxTime*60;
blackMessage = blackTime/3600 + ":" + (blackTime%3600)/60 + ":" + (blackTime%3600)%60;
whiteMessage = whiteTime/3600 + ":" + (whiteTime%3600)/60 + ":" + (whiteTime%3600)%60;
t.start();
this.repaint();
}
}
}catch(Exception e1){
JOptionPane.showMessageDialog(this," ");
}
}
if (x >= 380 && x <= 490 && y >= 190 && y <= 213)
// JOptionPane.showMessageDialog(this," ");
JOptionPane.showMessageDialog(this, " , , ");
if (x >= 380 && x <= 490 && y >= 310 && y <= 350)
{
//JOptionPane.showMessageDialog(this, " ");
int res = JOptionPane.showConfirmDialog(this," ?");
if(res == 0)
if(isBlack){
JOptionPane.showMessageDialog(this," !");
canPlay = false;
t.stop();
}
else
{
JOptionPane.showMessageDialog(this," !");
canPlay = false;
t.stop();
}
}
if (x >= 380 && x <= 490 && y >= 360 && y <= 400)
// JOptionPane.showMessageDialog(this," ");
JOptionPane.showMessageDialog(this, " :wuxinliulei; ");
if (x >= 380 && x <= 490 && y >= 410 && y <= 450)
// JOptionPane.showMessageDialog(this," ");
System.exit(0);
}
// }
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
private boolean checkWin() {
// 5
/*
* if(x+1<=18&&c==allChess[x+1][y]){ count++;
* if(x+2<=18&&c==allChess[x+2][y]){ count++; }
* if(x+3<=18&&c==allChess[x+3][y]){ count++; }
*
* }
*/
int c = allChess[x][y];
/*
* //x int i = 1; int count = 1; while (x+i<=18&&c == allChess[x +
* i][y]) { count++; i++; }
*
* i = 1; while (x-i>=0&&c == allChess[x - i][y]) { count++; i++; }
* if(count >= 5) return true;
*
* // i = 1; count = 1; while (y+i<=18&&c == allChess[x][y+i]) {
* count++; i++; }
*
* i = 1; while (y-i>=0&&c == allChess[x][y-i]) { count++; i++; }
* if(count >= 5) return true;
*
* // i = 1; count = 1; while (y-i>=0&&x+i<=18&&c ==
* allChess[x+i][y-i]) { count++; i++; }
*
* i = 1; while (y+i>=0&&x-i>=0&&c == allChess[x-i][y+i]) { count++;
* i++; } if(count >= 5) return true;
*
* // i = 1; count = 1; while (y-i>=0&&x-i>=0&&c ==
* allChess[x-i][y-i]) { count++; i++; }
*
* i = 1; while (y+i>=0&&x+i<=18&&c == allChess[x+i][y+i]) { count++;
* i++; } if(count >= 5) return true;
*/
int count;
count = this.checkCount(1, 0, c);
if (count >= 5)
return true;
count = this.checkCount(0, 1, c);
if (count >= 5)
return true;
count = this.checkCount(1, -1, c);
if (count >= 5)
return true;
count = this.checkCount(1, 1, c);
if (count >= 5)
return true;
return false;
}
private int checkCount(int xChange, int yChange, int c) {
int count = 1;
int tempx = xChange;
int tempy = yChange;
while (y + yChange >= 0 &&y+yChange<=18&&x+xChange>=0&& x + xChange <= 18
&& c == allChess[x + xChange][y + yChange]) {
count++;
if (xChange != 0)
xChange++;
if (yChange != 0) {
if (yChange < 0)
yChange--;
else
yChange++;
}
}
xChange = tempx;
yChange = tempy;
while (y - yChange >= 0 &&y-yChange<=18&&x-xChange>=0&& x - xChange <= 18
&& c == allChess[x - xChange][y - yChange]) {
count++;
if (xChange != 0)
xChange++;
if (yChange != 0) {
if (yChange < 0)
yChange--;
else
yChange++;
}
}
return count;
}
@Override
public void run() {
//
if(maxTime>0){
while(true){
if(isBlack){
blackTime--;
}
else{
whiteTime--;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
blackMessage = blackTime/3600 + ":" + (blackTime%3600)/60 + ":" + (blackTime%3600)%60;
whiteMessage = whiteTime/3600 + ":" + (whiteTime%3600)/60 + ":" + (whiteTime%3600)%60;
//t.resume();
this.repaint(); //
if(blackTime==0){
JOptionPane.showMessageDialog(this," , !");
canPlay = false;
t.stop();
}
if(whiteTime==0){
JOptionPane.showMessageDialog(this," , !");
canPlay = false;
t.stop();
}
//System.out.println(blackTime+" "+whiteTime);
//this.repaint();
}
}
}
}