五子棋
五子棋大致的流程跟我們之前寫的創意畫板差不多,大致的步驟分為
1.構建視窗
2.繪制棋盤和棋子并讓其重繪(視窗改變后不會消失)
3.需要判斷黑方下完,白方在下,以此進行
4.判斷輸贏的條件
5.添加按鈕和實作一些其他的功能(比如重新開始,認輸,悔棋,退出等)
代碼如下:
表單類:
package xq0817;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
//問題:1 重新開始不好使 2.下棋需要在右下角下棋 3.判斷輸贏 出現負數? 4. 悔棋沒寫 5.多執行緒寫下棋時間
public class GameUI extends JFrame {
int[][]piece=new int [16][16];
String[] btnstrs = {"開始游戲","認輸","悔棋","退出"};
public void initUI() {
GameUI jf =new GameUI();
jf.setTitle( "五子棋");
jf.setSize(900,800);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(3);
//添加流式布局管理器
FlowLayout fl = new FlowLayout();
jf.setLayout(fl);
//添加按鈕
jf.setVisible(true);
//畫筆:從表單上獲取畫筆物件,一定要在表單可見之后
Graphics g=jf.getGraphics();
GameMouse mouse = new GameMouse(g);
jf.addMouseListener( mouse);
jf.piece =mouse.piece ;
Dimension dim = new Dimension(100,40); //按鈕的尺寸
for (int i = 0; i < btnstrs.length; i++) { // 在回圈中呼叫下面封裝的方法
addButton(btnstrs[i],dim,Color.white,mouse,jf);
}
jf.setVisible(true);
}
public void addButton(String btnstr,Dimension dim, Color color, ActionListener mouse,JFrame jf){
JButton btn = new JButton(btnstr);
btn.setBackground(color);
btn.setPreferredSize(dim);
jf.add(btn);
btn.addActionListener(mouse);
}
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < 15; i++) {
g.setColor(Color.orange );
g.drawLine(160, 120+i*40, 720, 120+i*40); //畫棋盤橫線
g.drawLine(160+i*40, 120, 160+i*40,680); //畫棋盤豎線
}
for(int i=0; i<15; i++) { //重繪棋子
for(int j=0; j<15; j++) {
if(piece[i][j]==1) {
int tempX=i*40+160;
int tempY=j*40+120;
g.setColor(Color.black );
g.fillOval(tempX-20, tempY-20, 40, 40);
}else if (piece[i][j]==2) {
int tempX=i*40+160;
int tempY=j*40+120;
g.setColor(Color.white );
g.fillOval(tempX-20, tempY-20, 40, 40);
}
}
}
}
public static void main (String[] args) {
GameUI ui = new GameUI();
ui.initUI();
}
}
監聽器類:
package xq0817;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JOptionPane;
public class GameMouse implements MouseListener,ActionListener{
int[][]piece=new int [16][16];
public int turn=1; // 判斷下黑棋還是白棋
boolean canplay=true; //游戲結束后不能在下棋了
public Graphics g;
int x=0;
int y=0;
int i=0;
int j=0;
public GameMouse(Graphics g) {
this.g = g;
}
public void actionPerformed(ActionEvent e) {
String btnstr = e.getActionCommand();// 字串
System.out.println("點擊了"+btnstr);
if(btnstr.equals("開始游戲")){
int result = JOptionPane.showConfirmDialog(null, "是否重新開始游戲?");
if(result == 0) {
for(int i =0;i <=15;i++) {
for(int j =0; j<=15;j++) {
piece[i][j]=0;
}
}
}
}else if (btnstr.equals("認輸")){
int result=JOptionPane.showConfirmDialog(null, "是否認輸?");
if(result==0){
if(turn ==1) {
JOptionPane.showMessageDialog(null,"游戲結束,黑方認輸,白方獲勝!");
}else if(turn==2) {
JOptionPane.showMessageDialog(null,"游戲結束,白方認輸,黑方獲勝!");
}
}
}else if (btnstr.equals("退出")) {
int result = JOptionPane.showConfirmDialog(null, "是否退出游戲?");
if(result == 0){
System.exit(0);
}
}
}
public void mouseClicked(MouseEvent e) {
x = e.getX();
y = e.getY();
if (canplay==true) { //如果游戲沒結束 可以下棋
if(x>=160 && x<=720+10 && y >=120-10 &&y<=680+10) { //使棋子下在棋盤中
//二維陣列的下標
i=((x-160)/40);
j=((y-120)/40);
if(piece[i][j]==0) { //如果陣列為0,則可以下棋,(保證棋子不能下在有棋子的地方)
int tempx=((x-160)/40)*40+160;
int tempy=((y-120)/40)*40+120;
if(turn==1) { //下黑棋
g.setColor(Color.black);
g.fillOval(tempx-20, tempy-20, 40, 40);
piece[i][j]=1;
turn++;
}else if (turn==2) { // 下白棋
g.setColor(Color.white);
g.fillOval(tempx-20, tempy-20, 40, 40);
piece[i][j]=2;
turn--;
}
boolean winflag = this.checkWin(); //傳遞判斷輸贏方法的布爾資料
// System.out.println( winflag);
if(winflag == true) {
JOptionPane.showMessageDialog( null, "游戲結束"+(piece[i][j]==1? "黑方勝利!":"白方勝利!"));
canplay=false; // 游戲結束則不能在下棋
}
}else {
JOptionPane.showMessageDialog( null, "當前位置已經有子");
}
}else {
JOptionPane.showMessageDialog( null, "請在棋盤上落子");
}
}
}
public boolean checkWin() {
boolean flag=false;
int color=piece[i][j];
// 橫向五連判斷
int count=1;
int m=1;
while(color == piece[i+m][j]) {
count++;
m++;
}
m=1;
while(color == piece[i-m][j]) {
count++;
m++;
}
// System.out.println(count);
if(count >= 5) {
flag=true;
}
//縱向五連判斷
int count1=1;
int m1=1;
while(color == piece[i][j+m1]) {
count1++;
m1++;
}
m1=1;
while(color == piece[i][j-m]) {
count1++;
m1++;
}
if(count1 >= 5) {
flag=true;
}
//斜方向 (右上 左下)
int count2=1;
int m2=1;
while(color == piece[i+m2][j-m2]) {
count2++;
m2++;
}
m2=1;
while(color == piece[i-m2][j+m2]) {
count2++;
m2++;
}
if(count2 >= 5) {
flag=true;
}
//斜方向 (左上 右下)
int count3=1;
int m3=1;
while(color == piece[i-m3][j-m3]) {
count3++;
m3++;
}
m3=1;
while(color == piece[i+m3][j+m3]) {
count3++;
m3++;
}
if(count3 >= 5) {
flag=true;
}
return flag;
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
過幾天會把所有的功能全部完善一下,并進行一些其他的優化,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/295275.html
標籤:其他
上一篇:C語言小游戲 - 猜兇手
