本文目錄一覽:
- 1、java五子棋簡單的人機對戰程序邏輯
- 2、急!!! Java五子棋源代碼注釋
- 3、求五子棋的JAVA代碼,已寫出部分,我應該如何保存輸入過的坐標?
- 4、用eclipse編寫一個基於java的五子棋(與電腦對弈)
- 5、求一個簡單的JAVA五子棋代碼!! 網上複製的別來了!
- 6、JAVA五子棋程序代碼分析(1)
java五子棋簡單的人機對戰程序邏輯
兄弟,不是沒有落子行為,是一直有你看不出來吧,determineNextMove(int who)函數中獲得坐標時:int x = (int)Math.random() * 10;,這個x永遠都是0吧,應該是int x=(int)(Math.random()*10);優先順序搞錯了。所以你得到的坐標cell都是(0,0),通過後台列印System.out.println(cell.x + ” ” + cell.y);也可以看出來的吧。
急!!! Java五子棋源代碼注釋
package org.liky.game.frame;
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 {
// 取得屏幕的寬度
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
// 取得屏幕的高度
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
// 背景圖片
BufferedImage bgImage = null;
// 保存棋子的坐標
int x = 0;
int y = 0;
// 保存之前下過的全部棋子的坐標
// 其中數據內容 0: 表示這個點並沒有棋子, 1: 表示這個點是黑子, 2:表示這個點是白子
int[][] allChess = new int[19][19];
// 標識當前應該黑棋還是白棋下下一步
boolean isBlack = true;
// 標識當前遊戲是否可以繼續
boolean canPlay = true;
// 保存顯示的提示信息
String message = “黑方先行”;
// 保存最多擁有多少時間(秒)
int maxTime = 0;
// 做倒計時的線程類
Thread t = new Thread(this);
// 保存黑方與白方的剩餘時間
int blackTime = 0;
int whiteTime = 0;
// 保存雙方剩餘時間的顯示信息
String blackMessage = “無限制”;
String whiteMessage = “無限制”;
public FiveChessFrame() {
// 設置標題
this.setTitle(“五子棋”);
// 設置窗體大小
this.setSize(500, 500);
// 設置窗體出現位置
this.setLocation((width – 500) / 2, (height – 500) / 2);
// 將窗體設置為大小不可改變
this.setResizable(false);
// 將窗體的關閉方式設置為默認關閉後程序結束
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 為窗體加入監聽器
this.addMouseListener(this);
// 將窗體顯示出來
this.setVisible(true);
t.start();
t.suspend();
// 刷新屏幕,防止開始遊戲時出現無法顯示的情況.
this.repaint();
String imagePath = “” ;
try {
imagePath = System.getProperty(“user.dir”)+”/bin/image/background.jpg” ;
bgImage = ImageIO.read(new File(imagePath.replaceAll(“\\\\”, “/”)));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void paint(Graphics g) {
// 雙緩衝技術防止屏幕閃爍
BufferedImage bi = new BufferedImage(500, 500,
BufferedImage.TYPE_INT_RGB);
Graphics g2 = bi.createGraphics();
g2.setColor(Color.BLACK);
// 繪製背景
g2.drawImage(bgImage, 1, 20, this);
// 輸出標題信息
g2.setFont(new Font(“黑體”, Font.BOLD, 20));
g2.drawString(“遊戲信息:” + message, 130, 60);
// 輸出時間信息
g2.setFont(new Font(“宋體”, 0, 14));
g2.drawString(“黑方時間:” + blackMessage, 30, 470);
g2.drawString(“白方時間:” + whiteMessage, 260, 470);
// 繪製棋盤
for (int i = 0; i 19; i++) {
g2.drawLine(10, 70 + 20 * i, 370, 70 + 20 * i);
g2.drawLine(10 + 20 * i, 70, 10 + 20 * i, 430);
}
// 標註點位
g2.fillOval(68, 128, 4, 4);
g2.fillOval(308, 128, 4, 4);
g2.fillOval(308, 368, 4, 4);
g2.fillOval(68, 368, 4, 4);
g2.fillOval(308, 248, 4, 4);
g2.fillOval(188, 128, 4, 4);
g2.fillOval(68, 248, 4, 4);
g2.fillOval(188, 368, 4, 4);
g2.fillOval(188, 248, 4, 4);
/*
* //繪製棋子 x = (x – 10) / 20 * 20 + 10 ; y = (y – 70) / 20 * 20 + 70 ;
* //黑子 g.fillOval(x – 7, y – 7, 14, 14); //白子 g.setColor(Color.WHITE) ;
* g.fillOval(x – 7, y – 7, 14, 14); g.setColor(Color.BLACK) ;
* g.drawOval(x – 7, y – 7, 14, 14);
*/
// 繪製全部棋子
for (int i = 0; i 19; i++) {
for (int j = 0; j 19; j++) {
if (allChess[i][j] == 1) {
// 黑子
int tempX = i * 20 + 10;
int tempY = j * 20 + 70;
g2.fillOval(tempX – 7, tempY – 7, 14, 14);
}
if (allChess[i][j] == 2) {
// 白子
int tempX = i * 20 + 10;
int tempY = j * 20 + 70;
g2.setColor(Color.WHITE);
g2.fillOval(tempX – 7, tempY – 7, 14, 14);
g2.setColor(Color.BLACK);
g2.drawOval(tempX – 7, tempY – 7, 14, 14);
}
}
}
g.drawImage(bi, 0, 0, this);
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
/*
* System.out.println(“X:”+e.getX()); System.out.println(“Y:”+e.getY());
*/
if (canPlay == true) {
x = e.getX();
y = e.getY();
if (x = 10 x = 370 y = 70 y = 430) {
x = (x – 10) / 20;
y = (y – 70) / 20;
if (allChess[x][y] == 0) {
// 判斷當前要下的是什麼顏色的棋子
if (isBlack == true) {
allChess[x][y] = 1;
isBlack = false;
message = “輪到白方”;
} else {
allChess[x][y] = 2;
isBlack = true;
message = “輪到黑方”;
}
// 判斷這個棋子是否和其他的棋子連成5連,即判斷遊戲是否結束
boolean winFlag = this.checkWin();
if (winFlag == true) {
JOptionPane.showMessageDialog(this, “遊戲結束,”
+ (allChess[x][y] == 1 ? “黑方” : “白方”) + “獲勝!”);
canPlay = false;
}
} else {
JOptionPane.showMessageDialog(this, “當前位置已經有棋子,請重新落子!”);
}
this.repaint();
}
}
/* System.out.println(e.getX() + ” — ” + e.getY()); */
// 點擊 開始遊戲 按鈕
if (e.getX() = 400 e.getX() = 470 e.getY() = 70
e.getY() = 100) {
int result = JOptionPane.showConfirmDialog(this, “是否重新開始遊戲?”);
if (result == 0) {
// 現在重新開始遊戲
// 重新開始所要做的操作: 1)把棋盤清空,allChess這個數組中全部數據歸0.
// 2) 將 遊戲信息: 的顯示改回到開始位置
// 3) 將下一步下棋的改為黑方
for (int i = 0; i 19; i++) {
for (int j = 0; j 19; j++) {
allChess[i][j] = 0;
}
}
// 另一種方式 allChess = new int[19][19];
message = “黑方先行”;
isBlack = true;
blackTime = maxTime;
whiteTime = maxTime;
if (maxTime 0) {
blackMessage = maxTime / 3600 + “:”
+ (maxTime / 60 – maxTime / 3600 * 60) + “:”
+ (maxTime – maxTime / 60 * 60);
whiteMessage = maxTime / 3600 + “:”
+ (maxTime / 60 – maxTime / 3600 * 60) + “:”
+ (maxTime – maxTime / 60 * 60);
t.resume();
} else {
blackMessage = “無限制”;
whiteMessage = “無限制”;
}
this.canPlay = true;
this.repaint();
}
}
// 點擊 遊戲設置 按鈕
if (e.getX() = 400 e.getX() = 470 e.getY() = 120
e.getY() = 150) {
String input = JOptionPane
.showInputDialog(“請輸入遊戲的最大時間(單位:分鐘),如果輸入0,表示沒有時間限制:”);
try {
maxTime = Integer.parseInt(input) * 60;
if (maxTime 0) {
JOptionPane.showMessageDialog(this, “請輸入正確信息,不允許輸入負數!”);
}
if (maxTime == 0) {
int result = JOptionPane.showConfirmDialog(this,
“設置完成,是否重新開始遊戲?”);
if (result == 0) {
for (int i = 0; i 19; i++) {
for (int j = 0; j 19; j++) {
allChess[i][j] = 0;
}
}
// 另一種方式 allChess = new int[19][19];
message = “黑方先行”;
isBlack = true;
blackTime = maxTime;
whiteTime = maxTime;
blackMessage = “無限制”;
whiteMessage = “無限制”;
this.canPlay = true;
this.repaint();
}
}
if (maxTime 0) {
int result = JOptionPane.showConfirmDialog(this,
“設置完成,是否重新開始遊戲?”);
if (result == 0) {
for (int i = 0; i 19; i++) {
for (int j = 0; j 19; j++) {
allChess[i][j] = 0;
}
}
// 另一種方式 allChess = new int[19][19];
message = “黑方先行”;
isBlack = true;
blackTime = maxTime;
whiteTime = maxTime;
blackMessage = maxTime / 3600 + “:”
+ (maxTime / 60 – maxTime / 3600 * 60) + “:”
+ (maxTime – maxTime / 60 * 60);
whiteMessage = maxTime / 3600 + “:”
+ (maxTime / 60 – maxTime / 3600 * 60) + “:”
+ (maxTime – maxTime / 60 * 60);
t.resume();
this.canPlay = true;
this.repaint();
}
}
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(this, “請正確輸入信息!”);
}
}
// 點擊 遊戲說明 按鈕
if (e.getX() = 400 e.getX() = 470 e.getY() = 170
e.getY() = 200) {
JOptionPane.showMessageDialog(this,
“這個一個五子棋遊戲程序,黑白雙方輪流下棋,當某一方連到五子時,遊戲結束。”);
}
// 點擊 認輸 按鈕
if (e.getX() = 400 e.getX() = 470 e.getY() = 270
e.getY() = 300) {
int result = JOptionPane.showConfirmDialog(this, “是否確認認輸?”);
if (result == 0) {
if (isBlack) {
JOptionPane.showMessageDialog(this, “黑方已經認輸,遊戲結束!”);
} else {
JOptionPane.showMessageDialog(this, “白方已經認輸,遊戲結束!”);
}
canPlay = false;
}
}
// 點擊 關於 按鈕
if (e.getX() = 400 e.getX() = 470 e.getY() = 320
e.getY() = 350) {
JOptionPane.showMessageDialog(this,
“本遊戲由MLDN製作,有相關問題可以訪問”);
}
// 點擊 退出 按鈕
if (e.getX() = 400 e.getX() = 470 e.getY() = 370
e.getY() = 400) {
JOptionPane.showMessageDialog(this, “遊戲結束”);
System.exit(0);
}
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
private boolean checkWin() {
boolean flag = false;
// 保存共有相同顏色多少棋子相連
int count = 1;
// 判斷橫向是否有5個棋子相連,特點 縱坐標 是相同, 即allChess[x][y]中y值是相同
int color = allChess[x][y];
/*
* if (color == allChess[x+1][y]) { count++; if (color ==
* allChess[x+2][y]) { count++; if (color == allChess[x+3][y]) {
* count++; } } }
*/
// 通過循環來做棋子相連的判斷
/*
* int i = 1; while (color == allChess[x + i][y + 0]) { count++; i++; }
* i = 1; while (color == allChess[x – i][y – 0]) { count++; i++; } if
* (count = 5) { flag = true; } // 縱向的判斷 int i2 = 1 ; int count2 = 1 ;
* while (color == allChess[x + 0][y + i2]) { count2++; i2++; } i2 = 1;
* while (color == allChess[x – 0][y – i2]) { count2++; i2++; } if
* (count2 = 5) { flag = true ; } // 斜方向的判斷(右上 + 左下) int i3 = 1 ; int
* count3 = 1 ; while (color == allChess[x + i3][y – i3]) { count3++;
* i3++; } i3 = 1; while (color == allChess[x – i3][y + i3]) { count3++;
* i3++; } if (count3 = 5) { flag = true ; } // 斜方向的判斷(右下 + 左上) int i4 =
* 1 ; int count4 = 1 ; while (color == allChess[x + i4][y + i4]) {
* count4++; i4++; } i4 = 1; while (color == allChess[x – i4][y – i4]) {
* count4++; i4++; } if (count4 = 5) { flag = true ; }
*/
// 判斷橫向
count = this.checkCount(1, 0, color);
if (count = 5) {
flag = true;
} else {
// 判斷縱向
count = this.checkCount(0, 1, color);
if (count = 5) {
flag = true;
} else {
// 判斷右上、左下
count = this.checkCount(1, -1, color);
if (count = 5) {
flag = true;
} else {
// 判斷右下、左上
count = this.checkCount(1, 1, color);
if (count = 5) {
flag = true;
}
}
}
}
return flag;
}
// 判斷棋子連接的數量
private int checkCount(int xChange, int yChange, int color) {
int count = 1;
int tempX = xChange;
int tempY = yChange;
while (x + xChange = 0 x + xChange = 18 y + yChange = 0
y + yChange = 18
color == allChess[x + xChange][y + yChange]) {
count++;
if (xChange != 0)
xChange++;
if (yChange != 0) {
if (yChange 0)
yChange++;
else {
yChange–;
}
}
}
xChange = tempX;
yChange = tempY;
while (x – xChange = 0 x – xChange = 18 y – yChange = 0
y – yChange = 18
color == allChess[x – xChange][y – yChange]) {
count++;
if (xChange != 0)
xChange++;
if (yChange != 0) {
if (yChange 0)
yChange++;
else {
yChange–;
}
}
}
return count;
}
public void run() {
// TODO Auto-generated method stub
// 判斷是否有時間限制
if (maxTime 0) {
while (true) {
if (isBlack) {
blackTime–;
if (blackTime == 0) {
JOptionPane.showMessageDialog(this, “黑方超時,遊戲結束!”);
}
} else {
whiteTime–;
if (whiteTime == 0) {
JOptionPane.showMessageDialog(this, “白方超時,遊戲結束!”);
}
}
blackMessage = blackTime / 3600 + “:”
+ (blackTime / 60 – blackTime / 3600 * 60) + “:”
+ (blackTime – blackTime / 60 * 60);
whiteMessage = whiteTime / 3600 + “:”
+ (whiteTime / 60 – whiteTime / 3600 * 60) + “:”
+ (whiteTime – whiteTime / 60 * 60);
this.repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(blackTime + ” — ” + whiteTime);
}
}
}
}
求五子棋的JAVA代碼,已寫出部分,我應該如何保存輸入過的坐標?
棋盤是20X20的,你用String數組來表示的話,沒有棋子的棋盤上的qipan[x]y[]的值為空,該地方有黑棋的時候qipan[X][y]=『1』,白棋的話qipan[X][Y]=『2』,你無需記錄你下過的位置,只需畫棋盤時候該位置的值,為空的話可以繼續下棋子就可以了
用eclipse編寫一個基於java的五子棋(與電腦對弈)
基於java的五子棋:
主要功能代碼如下:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Main extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
JButton[][] point=new JButton[40][40];
int[][] state=new int[40][40];
int[][] value=new int[40][40];
int[] place=new int[2];
public Main(){
this.setTitle(“五子棋”);
this.setBounds(100,10,1000,1000);
this.setLayout(new GridLayout(30,30));
int i,j;
for(i=0;i=39;i++){
for(j=0;j=39;j++){
state[i][j]=0;
value[i][j]=0;
}
}
for(i=5;i=34;i++){
for (j=5;j=34;j++){
point[i][j]=new JButton(“”);
this.add(point[i][j]);
point[i][j].addActionListener(this);
}
}
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int i,j;
for(i=5;i=34;i++){
for(j=5;j=34;j++){
if(e.getSource()==point[i][j]){
point[i][j].setBackground(Color.RED);
state[i][j]=1;
point[i][j].setEnabled(false);
value[i][j]=0;
}
}
}
public static void main(String[] args)throws NullPointerException{
new Main();
}
else if(((state[i][j]==0)(state[i][j+1]==1)(state[i][j+2]==0))||
((state[i][j]==0)(state[i][j-1]==1)(state[i][j-2]==0))||
((state[i][j]==0)(state[i+1][j]==1)(state[i+2][j]==0))||
((state[i][j]==0)(state[i-1][j]==1)(state[i-2][j]==0))|| //空紅空
((state[i][j]==0)(state[i+1][j+1]==1)(state[i+2][j+2]==0))||
((state[i][j]==0)(state[i+1][j-1]==1)(state[i+2][j-2]==0))||
((state[i][j]==0)(state[i-1][j+1]==1)(state[i-2][j+2]==0))||
((state[i][j]==0)(state[i-1][j-1]==1)(state[i-2][j-2]==0))||
((state[i][j]==0)(state[i][j+1]==1)(state[i][j+2]==1)(state[i][j+3]==2))||
((state[i][j]==0)(state[i][j-1]==1)(state[i][j-2]==1)(state[i][j-3]==2))||
((state[i][j]==0)(state[i+1][j]==1)(state[i+2][j]==1)(state[i+3][j]==2))||
((state[i][j]==0)(state[i-1][j]==1)(state[i-2][j]==1)(state[i-3][j]==2))|| //空紅紅藍型
((state[i][j]==0)(state[i+1][j+1]==1)(state[i+2][j+2]==1)(state[i+3][j+3]==2))||
((state[i][j]==0)(state[i+1][j-1]==1)(state[i+2][j-2]==1)(state[i+3][j-3]==2))||
((state[i][j]==0)(state[i-1][j+1]==1)(state[i-2][j+2]==1)(state[i-3][j+3]==2))||
((state[i][j]==0)(state[i-1][j-1]==1)(state[i-2][j-2]==1)(state[i-3][j-3]==2))){
value[i][j]=2;
return value[i][j];
}
else if(((state[i][j]==0)(state[i][j+1]==2)(state[i][j+2]==0))||
((state[i][j]==0)(state[i][j-1]==2)(state[i][j-2]==0))||
((state[i][j]==0)(state[i+1][j]==2)(state[i+2][j]==0))||
((state[i][j]==0)(state[i-1][j]==2)(state[i-2][j]==0))||
((state[i][j]==0)(state[i+1][j+1]==2)(state[i+2][j+2]==0))||
((state[i][j]==0)(state[i+1][j-1]==2)(state[i+2][j-2]==0))||
((state[i][j]==0)(state[i-1][j+1]==2)(state[i-2][j+2]==0))||
((state[i][j]==0)(state[i-1][j-1]==2)(state[i-2][j-2]==0))||
((state[i][j]==0)(state[i][j+1]==2)(state[i][j+2]==2)(state[i][j+3]==1))||
((state[i][j]==0)(state[i][j-1]==2)(state[i][j-2]==2)(state[i][j-3]==1))||
((state[i][j]==0)(state[i+1][j]==2)(state[i+2][j]==2)(state[i+3][j]==1))||
((state[i][j]==0)(state[i-1][j]==2)(state[i-2][j]==2)(state[i-3][j]==1))||
((state[i][j]==0)(state[i+1][j+1]==2)(state[i+2][j+2]==2)(state[i+3][j+3]==1))||
((state[i][j]==0)(state[i+1][j-1]==2)(state[i+2][j-2]==2)(state[i+3][j-3]==1))||
((state[i][j]==0)(state[i-1][j+1]==2)(state[i-2][j+2]==2)(state[i-3][j+3]==1))||
((state[i][j]==0)(state[i-1][j-1]==2)(state[i-2][j-2]==2)(state[i-3][j-3]==1))){
value[i][j]=1;
return value[i][j];
}
else{
value[i][j]=0;
return value[i][j];
}
}
public int[] largestValue(int[][] value){
int max,i,j,l,k;
l=0;k=0;
max=0;
for(i=5;i34;i++){
for(j=5;j34;j++){
if(maxvalue[i][j]){
max=value[i][j];
place[0]=i;
place[1]=j;
}
}
}
return place;
}
}
運行結果如下圖所示:
求一個簡單的JAVA五子棋代碼!! 網上複製的別來了!
以下是現寫的 實現了兩人對戰 自己複製後運行把 沒什麼難度 類名 Games
import java.util.Scanner;
public class Games {
private String board[][];
private static int SIZE = 17;
private static String roles = “A玩家”;
//初始化數組
public void initBoard() {
board = new String[SIZE][SIZE];
for (int i = 0; i SIZE; i++) {
for (int j = 0; j SIZE; j++) {
// if(i==0){
// String str = “”;
// str += j+” “;
// board[i][j]= str;
// }else if(i!=0j==0){
// String str = “”;
// str += i+” “;
// board[i][j]= str;
// }else{
board[i][j] = “╋”;
// }
}
}
}
//輸出棋盤
public void printBoard() {
for (int i = 0; i SIZE; i++) {
for (int j = 0; j SIZE; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
}
//判斷所下棋子位置是否合理
public boolean isOk(int x, int y) {
boolean isRight = true;
if (x = 16 || x 1 || y = 16 | y 1) {
//System.out.println(“輸入錯誤,請從新輸入”);
isRight = false;
}
if (board[x][y].equals(“●”) || board[x][y].equals(“○”)) {
isRight = false;
}
return isRight;
}
//判斷誰贏了
public void whoWin(Games wz) {
// 從數組挨個查找找到某個類型的棋子就從該棋子位置向右,向下,斜向右下 各查找5連續的位置看是否為5個相同的
int xlabel;// 記錄第一次找到某個棋子的x坐標
int ylabel;// 記錄第一次找到某個棋子的y坐標
// ●○╋
// 判斷人是否贏了
for (int i = 0; i SIZE; i++) {
for (int j = 0; j SIZE; j++) {
if (board[i][j].equals(“○”)) {
xlabel = i;
ylabel = j;
// 橫向找 x坐標不變 y坐標以此加1連成字元串
String heng = “”;
if (i + 5 SIZE j + 5 SIZE) {
for (int k = j; k j + 5; k++) {
heng += board[i][k];
}
if (heng.equals(“○○○○○”)) {
System.out.println(roles+”贏了!您輸了!”);
System.exit(0);
}
// 向下判斷y不變 x逐增5 連成字元串
String xia = “”;
for (int l = j; l i + 5; l++) {
xia += board[l][j];
// System.out.println(xia);
}
if (xia.equals(“○○○○○”)) {
System.out.println(roles+”贏了!您輸了!”);
System.exit(0);
}
// 斜向右下判斷
String youxia = “”;
for (int a = 1; a = 5; a++) {
youxia += board[xlabel++][ylabel++];
}
if (youxia.equals(“○○○○○”)) {
System.out.println(roles+”贏了!您輸了!”);
System.exit(0);
}
}
}
}
}
// 判斷電腦是否贏了
for (int i = 0; i SIZE; i++) {
for (int j = 0; j SIZE; j++) {
if (board[i][j].equals(“●”)) {
xlabel = i;
ylabel = j;
// 橫向找 x坐標不變 y坐標以此加1連成字元串
String heng = “”;
if (j + 5 SIZE i + 5 SIZE) {
for (int k = j; k j + 5; k++) {
heng += board[i][k];
}
if (heng.equals(“●●●●●”)) {
System.out.println(roles+”贏輸了!您輸了!”);
System.exit(0);
}
// 向下判斷y不變 x逐增5 連成字元串
String xia = “”;
for (int l = i; l i + 5; l++) {
xia += board[l][ylabel];
// System.out.println(xia);
}
if (xia.equals(“●●●●●”)) {
System.out.println(roles+”贏了!您輸了!”);
System.exit(0);
}
// 斜向右下判斷
String youxia = “”;
for (int a = 1; a = 5; a++) {
youxia += board[xlabel++][ylabel++];
}
if (youxia.equals(“●●●●●”)) {
System.out.println(roles+”贏了!您輸了!”);
System.exit(0);
}
}
}
}
}
}
public static void main(String[] args) {
Games wz = new Games();
Scanner sc = new Scanner(System.in);
wz.initBoard();
wz.printBoard();
while (true) {
System.out.print(“請”+roles+”輸入X,Y坐標,必須在0-15範圍內,xy以空格隔開,輸入16 16結束程序”);
int x = sc.nextInt();
int y = sc.nextInt();
if (x == SIZE y == SIZE) {
System.out.println(“程序結束”);
System.exit(0);
}
if (x SIZE || x 0 || y SIZE | y 0) {
System.out.println(“輸入錯誤,請從新輸入”);
continue;
}
//如果roles是A玩家 就讓A玩家下棋,否則就讓B玩家下棋。
if (wz.board[x][y].equals(“╋”)roles.equals(“A玩家”)) {
wz.board[x][y] = “○”;
wz.printBoard();
//判斷輸贏
wz.whoWin(wz);
}else if(wz.board[x][y].equals(“╋”)roles.equals(“B玩家”)){
wz.board[x][y] = “●”;
wz.printBoard();
//判斷輸贏
wz.whoWin(wz);
} else {
System.out.println(“此處已經有棋子,從新輸入”);
continue;
}
if(roles.equals(“A玩家”)){
roles = “B玩家”;
}else if(roles.equals(“B玩家”)){
roles = “A玩家”;
}
}
}
}
JAVA五子棋程序代碼分析(1)
樓主要是覺得看的不舒服可以拷到記事本里看~ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.awt.Color; //這一段import就不說了,下面要用到的就import進來 public class wuziqi extends Applet implements ActionListener,MouseListener,MouseMotionListener,ItemListener //繼承Applet表明是個applet,後面的介面必須要實現每個介面的所有方法 { int color_Qizi=0;//旗子的顏色標識 0:白子 1:黑子 int intGame_Start=0;//遊戲開始標誌 0未開始 1遊戲中 int intGame_Body[][]=new int[16][16]; //設置棋盤棋子狀態 0 無子 1 白子 2 黑子 Button b1=new Button(“遊戲開始”); Button b2=new Button(“重置遊戲”); //兩個按鈕 Label lblWin=new Label(” “); //這個label用來顯示最後輸贏信息的,先留空 Checkbox ckbHB[]=new Checkbox[2]; //用來表明選擇黑氣或白棋先走的checkbox CheckboxGroup ckgHB=new CheckboxGroup(); //兩個checkbox必須放在同一個checkboxgroup里才能做到單選 public void init() //初始化,堆砌界面 { setLayout(null); //不設布局管理器 addMouseListener(this); //將本類作為滑鼠事件的介面響應滑鼠動作 add(b1); //將事先定義好的第一個按鈕添加入界面 b1.setBounds(330,50,80,30); //設置第一個按鈕左上角的位置和大小 b1.addActionListener(this); //將本類作為按鈕事件的介面響應按鈕動作 add(b2); //將事先定義好的第二個按鈕添加進去 b2.setBounds(330,90,80,30); /設置第二個按鈕左上角的位置和大小 b2.addActionListener(this); //將本類作為按鈕事件的介面響應按鈕動作 ckbHB[0]=new Checkbox(“白子先”,ckgHB,false); //new一個checkbox ckbHB[0].setBounds(320,20,60,30); //設置左上角位置和大小 ckbHB[1]=new Checkbox(“黑子先”,ckgHB,false); //new第二個checkbox ckbHB[1].setBounds(380,20,60,30); //設置左上角位置和大小 add(ckbHB[0]); //將第一個checkbox加入界面 add(ckbHB[1]); //將第二個checkbox加入界面 ckbHB[0].addItemListener(this); //將本類作為其事件介面來響應選中動作 ckbHB[1].addItemListener(this); //將本類作為其事件介面來響應選中動作 add(lblWin); //將標籤加入界面 lblWin.setBounds(330,130,80,30); //設置標籤的左上角位置和大小 Game_start_csh(); //調用遊戲初始化 } public void itemStateChanged(ItemEvent e) //ItemListener介面中的方法,必須要有 { if (ckbHB[0].getState()) //選擇黑子先還是白子先 { color_Qizi=0; //白棋先 } else { color_Qizi=1; //黑棋先 } } public void actionPerformed(ActionEvent e) //ActionListener介面中的方法,也是必須的 { Graphics g=getGraphics(); //這句話貌似可以去掉,g是用來畫圖或者畫界面的 if (e.getSource()==b1) //如果動作的來源是第一個按鈕 { Game_start(); //遊戲開始 } else //否則 { Game_re(); //遊戲重新開始 } } public void mousePressed(MouseEvent e){} //MouseListener介面中的方法,用不到所以留個空,但一定要有 public void mouseClicked(MouseEvent e) //滑鼠單擊時 { Graphics g=getGraphics(); //獲得畫筆 int x1,y1; x1=e.getX(); //單擊處的x坐標 y1=e.getY(); //單擊處的y坐標 if (e.getX()20 || e.getX()300 || e.getY()20 || e.getY()300) //在棋盤範圍之外 { return; //則這是不能走棋的,直接返回 } //下面這兩個if和兩個賦值的作用是將x和y坐標根據舍入原則修改成棋盤上格子的坐標 if (x1%2010) { x1+=20; } if(y1%2010) { y1+=20; } x1=x1/20*20; y1=y1/20*20; set_Qizi(x1,y1); //在棋盤上畫上一個棋子 } public void mouseEntered(MouseEvent e){} //MouseListener介面中的方法,用不到所以留個空,但一定要有 public void mouseExited(MouseEvent e){} //MouseListener介面中的方法,用不到所以留個空,但一定要有 public void mouseReleased(MouseEvent e){} //MouseListener介面中的方法,用不到所以留個空,但一定要有 public void mouseDragged(MouseEvent e){} //MouseListener介面中的方法,用不到所以留個空,但一定要有 public void mouseMoved(MouseEvent e){} //MouseListener介面中的方法,用不到所以留個空,但一定要有 public void paint(Graphics g) //重繪和applet程序裝載的時候會調用這個繪製的過程 { draw_qipan(g); //畫棋盤 }
原創文章,作者:YZJUA,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/329408.html