bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

五子棋悔棋java代碼,五子棋悔棋java代碼

用JAVA實現五子棋悔棋代碼

保存成.java文件直接運行編譯

成都創新互聯公司專注為客戶提供全方位的互聯網綜合服務,包含不限于成都網站建設、成都做網站、平陸網絡推廣、微信小程序開發、平陸網絡營銷、平陸企業策劃、平陸品牌公關、搜索引擎seo、人物專訪、企業宣傳片、企業代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;成都創新互聯公司為所有大學生創業者提供平陸建站搭建服務,24小時服務熱線:18980820575,官方網址:vcdvsql.cn

這是我回答的別人問題的答案地址

import java.util.*;

import java.io.*;

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

import javax.swing.*;

class Gobang extends JFrame implements Runnable, ActionListener

{

final static int Player=1;

final static int AI =-1;

ClassLoader cl = this.getClass().getClassLoader();

Toolkit tk = Toolkit.getDefaultToolkit();

int length=14, game_state, winner, check, step;

int grid[][] = new int[length][length];

int locX, locY /* ?囪?竚 */, count /* 硈囪計 */, x, y /* 既??竚 */, displace_x=0, displace_y=0 /* ?簿秖 */, direction;

ArrayList steps = new ArrayList(); /* 癘魁囪˙ */

JPopupMenu control_menu = new JPopupMenu(); /* ?齡匡蟲?? */

JMenuItem[] command = new JMenuItem[4];

String[] command_str={"?囪", "?郎", "弄郎", "?秨"};

int[][] dir = { {-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1} };

boolean[] dir2 = new boolean[8];

boolean turn;

String message;

final JDialog dialog = new JDialog(this, "叫匡?", true);

Font font=new Font("new_font", Font.BOLD, 20);

Grid grids[][] = new Grid[length][length];

Image white= tk.getImage(cl.getResource("res/white.png"));

Image black= tk.getImage(cl.getResource("res/black.png"));

Image title= tk.getImage(cl.getResource("res/title.png"));

Image temp;

JPanel boardPanel, bigpanel;

JRadioButton[] choice = new JRadioButton[2];

final static int Start =0;

final static int Select =1;

final static int Playing =2;

final static int End =3;

final static int nil=-1; /* 礚よ? */

final static int oblique_1 =0; /* ???オ? */

final static int oblique_2 =1; /* オ???? */

final static int horizontal =2; /* 絹? */

final static int vertical=3; /* ?? */

Gobang()

{

super("き?囪");

boardPanel = new JPanel();

boardPanel.setLayout(new GridLayout(length, length, 0, 0));

boardPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

for(int i=0; ilength; i++)

for(int j=0; jlength; j++)

{

grids[i][j] = new Grid(i, j);

boardPanel.add(grids[i][j]);

}

bigpanel = new JPanel();

bigpanel.add(boardPanel, BorderLayout.CENTER);

getContentPane().add(bigpanel, BorderLayout.SOUTH);

game_state=Start;

ThreadStart();

dialog.setSize(160, 100);

dialog.setResizable(false);

dialog.setLocationRelativeTo(null);

ButtonGroup choice_group = new ButtonGroup();

JPanel choice_menu = new JPanel();

choice[0] = new JRadioButton("堵?", new ImageIcon(black), true);

choice[1] = new JRadioButton("フ?", new ImageIcon(white));

for(int i=0; ichoice.length; i++)

{

choice_menu.add(choice[i]);

choice_group.add(choice[i]);

}

for(int i=0; icommand.length; i++)

{

command[i] =new JMenuItem(command_str[i]);

command[i].addActionListener(this);

control_menu.add(command[i]);

}

JButton select = new JButton("絋)");

JPanel select_menu = new JPanel();

select_menu.add(select);

select.addActionListener(this);

dialog.getContentPane().add(choice_menu, BorderLayout.NORTH);

dialog.getContentPane().add(select_menu, BorderLayout.CENTER);

setIconImage(title);

setResizable(false);

setSize(300, 335);

setVisible(true);

setLocationRelativeTo(null);

}

public static void main(String[] arg)

{

Gobang application = new Gobang();

application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void actionPerformed(ActionEvent event)

{

if( event.getSource().equals(command[0]) )

{

if(steps.size()!=0)

undo();

}

else if(event.getSource().equals(command[1]) )

Save();

else if(event.getSource().equals(command[2]) )

Load();

else if(event.getSource().equals(command[3]) )

ReStart();

else

{

if(choice[1].isSelected())

{

temp=white;

white=black;

black=temp;

}

dialog.dispose();

}

}

public void WinCheck()

{

check = turn? Player: AI;

direction=oblique_1;

WinCheck2();

}

public void WinCheck2()

{

count=1;

switch(direction)

{

case oblique_1: displace_x=1;

displace_y=-1;

direction=oblique_2;

break;

case oblique_2: displace_x=displace_y=1;

direction=horizontal;

break;

case horizontal: displace_x=1;

displace_y=0;

direction=vertical;

break;

case vertical: displace_x=0;

displace_y=1;

direction=nil;

break;

}

x=locX+displace_x;

y=locY+displace_y;

while(x=0 xlength y=0 ylength grid[x][y]==check)

{

count=count+1;

x=x+displace_x;

y=y+displace_y;

}

x=locX-displace_x;

y=locY-displace_y;

while(x=0 xlength y=0 ylength grid[x][y]==check)

{

count=count+1;

x=x-displace_x;

y=y-displace_y;

}

if(count=5)

{

game_state=End;

winner=check;

ThreadStart();

}

else if(direction!=nil)

WinCheck2();

}

public void ReStart()

{

for(int i=0; ilength; i++)

for(int j=0; jlength; j++)

grids[i][j].Initial();

winner=0;

steps.clear();

game_state=Playing;

}

public void gobangRandom()

{

displace_y=0;

do

{

displace_x=(int)(Math.random()*8);

x =locX+dir[displace_x][0];

y =locY+dir[displace_x][1];

if(!dir2[displace_x])

{

displace_y=displace_y+1;

dir2[displace_x]=true;

}

}while((x0 || x=length || y0 || y=length || grid[x][y]!=0) displace_y8);

for(int i=0; i8; i++)

dir2[i]=false;

if(x=0 xlength y=0 ylength grid[x][y]==0)

setMark(x, y);

else

gobangRandom2();

}

private void gobangRandom2()

{

do

{

x=(int)(Math.random()*length);

y=(int)(Math.random()*length);

}while(grid[x][y]!=0);

setMark(x, y);

}

private void gobangAI()

{

boolean play=true;

for(int i=2; i0; i--)

{

play=!play;

gobangAI4(play);

if(turn)

return;

}

for(int i=7; i=4; i--)

{

play=!play;

step=i/2;

gobangAI2(play);

if(turn)

return;

}

gobangAI5();

if(!turn)

gobangRandom();

}

private void gobangAI2(boolean player)

{

check = player? Player: AI;

for(int i=0; ilength; i++)

for(int j=0; jlength; j++)

{

if(turn)

break;

if(grid[i][j] == check)

{

count=1;

direction=oblique_1;

gobangAI3(i, j);

}

}

}

private void gobangAI3(int x, int y)

{

if(count==1)

{

locX=x;

locY=y;

}

switch(direction)

{

case oblique_1: displace_x=1;

displace_y=-1;

break;

case oblique_2: displace_x=displace_y=1;

break;

case horizontal: displace_x=1;

displace_y=0;

break;

case vertical: displace_x=0;

displace_y=1;

break;

}

x=x+displace_x;

y=y+displace_y;

if(x=0 xlength y=0 ylength grid[x][y]==check)

{

count=count+1;

if(count==step)

{

if(x+displace_x=0 x+displace_xlength y+displace_y=0 y+displace_ylength grid[x+displace_x][y+displace_y]==0)

{

if(x-step*displace_x0 || x-step*displace_x=length || y-step*displace_y0 || y-step*displace_y=length || !gobang_SpaceAI(x+displace_x, y+displace_y) || (grid[x-step*displace_x][y-step*displace_y]!=0 step3) ) /* 癸翴?猵 */

gobangAI3_2();

else

setMark2(x+displace_x, y+displace_y);

}

else

gobangAI3_2();

}

else

gobangAI3(x, y);

}

else

gobangAI3_2();

}

private void gobangAI3_2()

{

if(direction!=vertical)

{

count=1;

direction=direction+1;

gobangAI3(locX, locY);

}

}

private void gobangAI4(boolean player)

{

check = player? Player: AI;

for(int i=0; ilength; i++)

for(int j=0; jlength; j++)

{

if(turn)

break;

if(grid[i][j]==0)

{

direction=oblique_1;

locX=i;

locY=j;

gobangAI4();

}

}

}

private void gobangAI4()

{

count=0;

switch(direction)

{

case oblique_1: displace_x=1;

displace_y=-1;

direction=oblique_2;

break;

case oblique_2: displace_x=displace_y=1;

direction=horizontal;

break;

case horizontal: displace_x=1;

displace_y=0;

direction=vertical;

break;

case vertical: displace_x=0;

displace_y=1;

direction=nil;

break;

}

x=locX+displace_x;

y=locY+displace_y;

while(x=0 xlength y=0 ylength grid[x][y]==check)

{

count=count+1;

x=x+displace_x;

y=y+displace_y;

}

x=locX-displace_x;

y=locY-displace_y;

while(x=0 xlength y=0 ylength grid[x][y]==check)

{

count=count+1;

x=x-displace_x;

y=y-displace_y;

}

if(count=4)

setMark(locX, locY);

else if(direction!=nil)

gobangAI4();

}

private void gobangAI5()

{

for(int i=0; ilength; i++)

for(int j=0; jlength; j++)

{

if(turn)

break;

if(grid[i][j]==-1)

{

direction=oblique_1;

locX=i;

locY=j;

gobangAI5_2();

}

}

}

/* 璸衡硈絬?フ耞糷 */

private void gobangAI5_2()

{

count=0;

switch(direction)

{

case oblique_1: displace_x=1;

displace_y=-1;

direction=oblique_2;

break;

case oblique_2: displace_x=displace_y=1;

direction=horizontal;

break;

case horizontal: displace_x=1;

displace_y=0;

direction=vertical;

break;

case vertical: displace_x=0;

displace_y=1;

direction=nil;

break;

}

x=locX+displace_x;

y=locY+displace_y;

while(x=0 xlength y=0 ylength grid[x][y]==0 count4)

{

count=count+1;

x=x+displace_x;

y=y+displace_y;

}

x=locX-displace_x;

y=locY-displace_y;

if(count==4 x=0 xlength y=0 ylength grid[x][y]==0)

setMark(locX+displace_x, locY+displace_y);

else if(count1)

{

count = count==4? 3: count;

while(x=0 xlength y=0 ylength grid[x][y]==0 count4)

{

count=count+1;

x=x-displace_x;

y=y-displace_y;

}

if(count==4)

setMark(locX+displace_x, locY+displace_y);

else if(direction!=nil)

gobangAI5_2();

}

else if(direction!=nil)

gobangAI5_2();

}

private boolean gobang_SpaceAI(int x, int y) /* 璸衡逞緇?フ?竚 */

{

int space=0;

do

{

space=space+1;

x=x+displace_x;

y=y+displace_y;

}while(x=0 xlength y=0 ylength grid[x][y]==0 space4);

if(space+count=5) /* 逞緇?フ場だΤ硈絬ぇ?? */

return true;

else

{

space=0;

x=locX-displace_x;

y=locY-displace_y;

while(x=0 xlength y=0 ylength grid[x][y]==0 space4)

{

space=space+1;

x=x-displace_x;

y=y-displace_y;

}

if(space+count=5) /* 逞緇?フ場だΤ硈絬ぇ?? */

return true;

else

return false;

}

}

public void ThreadStart()

{

new Thread(this).start();

}

private void setMark(int x, int y)

{

steps.add(grids[x][y]);

grids[x][y].setValue(-1);

WinCheck();

turn=true;

}

/* 璸衡?ê?よ?Τ?隔 */

private void setMark2(int x, int y)

{

int space=0, temp_x=x, temp_y=y;

do

{

space=space+1;

temp_x=temp_x+displace_x;

temp_y=temp_y+displace_y;

}while(temp_x=0 temp_xlength temp_y=0 temp_ylength grid[temp_x][temp_y]==0 space4);

if(space+step=5)

setMark(x, y);

else

setMark(x-(step+1)*displace_x, y-(step+1)*displace_y); /* э?癸翴 */

}

public void run()

{

try

{

switch(game_state)

{

case Start: Thread.sleep(2000);

dialog.show();

game_state=Playing;

repaint();

break;

case End: repaint();

Thread.sleep(1500);

ReStart();

repaint();

break;

}

}

catch(InterruptedException ex)

{

}

}

public void paint(Graphics g)

{

super.paint(g);

switch(game_state)

{

case Start: g.drawString("2005.7 by Yu Lin Tao", 91, 240);

g.setFont(font);

g.setColor(Color.BLUE);

g.drawString("Gobang", 112, 120);

g.drawImage(title, 134, 135, this);

break;

case Playing:

case End:

g.drawString("?產: 筿福:", 105, 40);

g.drawImage(black, 132, 30, this);

g.drawImage(white, 182, 30, this);

if(winner!=0)

{

g.setFont(font);

g.setColor(Color.RED);

message = winner==1? "?墓?":"?塊?";

g.drawString(message, 120, 185);

}

break;

}

}

public void undo()

{

if(steps.size()0)

{

for(int i=0; i2; i++)

{

((Grid)steps.get(steps.size()-1)).Initial();

steps.remove(steps.size()-1);

}

}

}

private void Save()

{

String str;

try

{

File file = new File("innings.men");

file.createNewFile();

BufferedWriter write = new BufferedWriter(new FileWriter(file));

for(int i=0; isteps.size(); i++)

{

str=(((Grid)steps.get(i)).getLocX())+","+(((Grid)steps.get(i)).getLocY())+","+(((Grid)steps.get(i)).getValue());

write.write(str);

write.newLine();

}

write.close();

}

catch(Exception ex)

{

}

}

private void Load()

{

try

{

String[] step_array;

String str;

File file = new File("innings.men");

if(!file.exists())

{

JOptionPane.showMessageDialog(null, "⊿Τ?郎");

return;

}

BufferedReader read = new BufferedReader(new FileReader(file));

ReStart();

while(read.ready())

{

str=read.readLine();

step_array=str.split(",");

grids[Integer.parseInt(step_array[0])][Integer.parseInt(step_array[1])].setValue(Integer.parseInt(step_array[2]));

steps.add(grids[Integer.parseInt(step_array[0])][Integer.parseInt(step_array[1])]);

}

}

catch(Exception ex)

{

}

}

private class Grid extends JPanel implements MouseListener

{

int x, y, value;

boolean selected;

public Grid(int x, int y)

{

this.x=x;

this.y=y;

addMouseListener(this);

}

public void mousePressed(MouseEvent event)

{

if(game_state==Playing)

{

int button=event.getButton();

if(button==MouseEvent.BUTTON1)

{

if(value==0)

{

steps.add(this);

setValue(1);

WinCheck();

turn=false;

if(game_state==Playing)

gobangAI();

}

}

else

control_menu.show(this, event.getX(), event.getY());

}

}

public void mouseEntered(MouseEvent event)

{

if(game_state==Playing)

setSelected(true);

}

public void mouseExited(MouseEvent event)

{

if(game_state==Playing)

setSelected(false);

}

public void mouseClicked(MouseEvent event)

{

}

public void mouseReleased(MouseEvent event)

{

}

public Dimension getPreferredSize()

{

return new Dimension(20, 20);

}

public Dimension getMinimumSize()

{

return getPreferredSize();

}

public int getLocX()

{

return x;

}

public int getLocY()

{

return y;

}

public int getValue()

{

return value;

}

public void Initial()

{

value=grid[x][y]=0;

selected=false;

repaint();

}

public void setValue(int new_value)

{

locX=x;

locY=y;

value=grid[x][y]=new_value;

repaint();

}

public void setSelected(boolean select)

{

selected=select;

repaint();

}

public void paintComponent(Graphics g)

{

super.paintComponent(g);

if(game_state!=Start game_state!=Select)

{

g.drawLine(0, 10, 19, 10);

g.drawLine(10, 0, 10, 19);

/* 礶娩絬 */

g.setColor(Color.BLUE);

if(x==0)

g.drawLine(0, 0, 19, 0);

else if(x==length-1)

g.drawLine(0, 19, 19, 19);

if(y==0)

g.drawLine(19, 0, 19, 19);

else if(y==length-1)

g.drawLine(0, 0, 0, 19);

if(selected)

setBackground(Color.WHITE);

else

setBackground(Color.LIGHT_GRAY);

}

//g.drawString(x+" "+y, 1, 10);

if(value!=0)

{

temp = value==1? black: white;

g.drawImage(temp, 4, 4, this);

}

}

}

}

系統框圖如下 java實現五子棋程序 可以實現人人對戰 人機對戰 簡單功能 悔棋 認輸

一、實驗題目

五子棋游戲。

二、問題分析

五子棋是雙人博弈棋類益智游戲,由圍棋演變而來,屬純策略型。棋盤通常15*15,即15行,15列,共225個交叉點,即棋子落點;棋子由黑白兩色組成,黑棋123顆,白棋122顆。游戲規則為黑先白后,誰先五子連成一條直線誰贏,其中直線可以是橫的、縱的、45度、135度。

本次Java編程我的目的是現實人機對戰,即游戲者一方是人,另一方計算機。這就要求程序不僅要具備五子棋的基本界面,還要編程指導計算機與人進行對弈。為了使程序盡可能智能,我采用了貪心策略、傳統搜索算法、極大極小博弈樹算法,對應游戲玩家的3個等級:簡單、中等、困難。

三、功能設計

我的程序基本功能是實現人機對弈五子棋。人和電腦交替下棋,誰先五子連成一條直線誰就贏。下面是我程序的功能模塊:

1.等級設置

核心功能是實現不同策略與算法的對比運用,純貪心策略實現簡單等級對手,直接搜索算法實現中等等級對手,極大極小博弈樹算法實現困難等級對手。對應程序中的3選1單選按鈕。

2.悔棋功能

模擬棧機制實現人悔棋,不限步長的悔棋。對應程序中的悔棋按鈕。

3.棋面繪制

根據不同機計算機的屏幕分辨率,繪制逼真的棋盤。

4.圖片引入

兩張古典的人物圖片,生動模擬對弈雙方。人物圖片旁的黑白棋缽圖片顯示黑白棋歸屬。

5.背景設置

支持用戶選擇背景,包括棋盤、棋盤邊框、窗口邊框,彰顯個性。

6.音樂播放

下棋時有棋子落地的聲音,一方勝利時有五子連成一片的聲音。同時在設置背景時相應的改變整個對弈過程中的背景音樂。

7.時間顯示

在棋盤正上方有一模擬文本框顯示當前棋局用時。

8.其他小功能

支持和棋、認輸、開啟新游戲、退出游戲等操作。

四、數據結構與算法設計

數據結構部分

1.當前棋局的存儲結構

我的五子棋程序選擇通常用到的15行*15列棋盤,可以開二維數組PositionFlag?=?new?int[15][15],PositionFlag[i][j]為0表示(i,j)點尚無棋,為1表示(i,j)點是人的棋子,為2表示(i,j)點是機器的棋子。之所以選擇二維數組,主要原因有兩點:

1.本程序需要頻繁隨機訪問15*15的交叉點,對應查詢該點狀態以及改變該點狀態,隨機訪問是數組的特點。

2.15*15=225開二維數組的內存需求相對現在內存為2G及以上的計算機完全可以接受,且數組實現簡單、操作方便。

基于以上兩點,盡管創建動態的順序表—鏈表可能可以節省少量內存(可以只存當前有棋的點,原數組對應位置為0的點可以不存),但選擇數組的優勢完全在上述兩點體現了出來。

2.實現悔棋操作的數據結構

由于每次悔棋只需回退當前幾步,后進先出原則,這正是棧這種典型數據結構的設計思想,于是我選擇棧。我自己先寫了用自定義數組模擬的棧,但由于是學Java語言且由于悔棋的存儲空間需要隨當前步數增大而增大(由于每局最多下225步,即最多要悔225步,所以自己開個225的數組完全可以避免存儲空間自增長的問題且內存完全可以接受,之所以不用自定義數組而用ArrayList類主要是為了嘗試Java中STL的用法),所有我最終改為用Java類庫中的ArrayList類。

確定用ArrayList類實現棧機制后就必須考慮每個ArrayList單元具體存儲什么。剛開始我存儲的是當前的棋局,即整個局面,而每個局面對應一個二維數組,這樣是很占用內存的。試想一下,在最壞情況下,225個ArrayList單元,每個單元存放一個15*15的二維數組,盡管225*15*15在Java的內存管理機制下不會爆棧,但也是極不劃算的。之所以說不劃算,是因為有更好的解決方案。由于每次悔棋只是在回退倒數一步,多步悔棋只需循環回退,所以可以只存儲當前棋局最后一步的下法,對應一個二維點,完全可以自定義一個二維坐標類chessOneStep。

算法設計部分

Java語言是面向對象的語言。我在進行五子棋游戲編程是總共傳創建了11個自定義的類。在編寫程序的過程中,我有一個明顯的體驗就是面向對象編程就是一項有關對象設計和對象接口技術,很多關鍵的技術就是如何設計自定義的對象。

下面我先概括給出我的所有類的作用:

1.mainFrame類:主框架類,我應用程序的入口;

2.chessPositon類:主控類,這個類是我程序的核心類,負責控制雙方的下棋,以及調用其他的類完成當前棋局的顯示繪制;

3.chessPanel類:面板類,調用其他底層類完成當前棋局的顯示繪制;

4.chessBoard類:棋盤繪制類,負責棋盤的繪制;

5.chessImage類:文件類,包含各種資源(背景圖片、背景音樂)以及靜態全局變量(public?static?Type);

6.chessButton類:組件類,定義各種組件,包括按鈕、單選按鈕、文本框等;

7.chessMusic類:音樂類,負責調用Java庫類完成背景音樂、下棋音樂、取勝音樂等的播放;

8.chessPiece類:棋局類,定義棋局二維數組數據結構并完成相關操作;

9.chessList類:棧類,完成悔棋等操作;

10.?chessOneStep類:棋子類,定義每步坐標以及下在該處獲得的估價值;

11.myCompare類:排序類,完成chessOneStep類的自定義排序

詳細設計

1.mainFrame類

作為我的五子棋程序的主類,mainFrame類主要實例化相關的對象,如chessbutton,chessborad等,從而完成框架的創建。更重要的是實例化chessposition,這是本程序的核心類,控制游戲雙方行棋過程完成人機互動下棋,然后將MyChessPosition與鼠標響應addMouseListener()關聯起來。

2.chessMusic類

一個好的游戲必須給人一種身臨其境的感覺,而聲音是營造這種氛圍的重要因素。參照網上各游戲運行商的音樂配置,我選擇相關逼真的聲音。包括背景音樂、下棋棋子落到棋盤發出的聲音以及一方勝出的配樂。所有這些功能的實現,依賴于自定義的chessMusic類,采用AudioInputStream配合Clip的方式完成音樂播放的軟硬件工作,然后定義兩個接口chessmusic(String?Name)和Stop(),前者完成播放功能,后者完成關閉當前音樂功能。因為音頻文件相對較大,而我的程序提供在不同背景樂之間切換的功能,所以在打開另一個音頻文件之前必須關閉前一個正在播放的音頻文件,防止出現溢出。

3.chessImage類

適當的動畫或圖片能給游戲玩家帶來美的體驗。所以我的五子棋程序界面在不失和諧的前提下引入了盡可能多的圖片,包括對弈雙方、棋缽等。圖片引入的具體工作通過語句import?javax.imageio.ImageIO完成。同時,由于圖片要在用到它的類中被訪問,為了避免頻繁調用函數,我直接將圖片相關聯的對象定義為public?static,表明是公用的、靜態的。進一步引申開去,我將程序中用到的靜態全局變量都定義在chessImage類中。具體如下:

public?static?Date?begin;//每局開始時間

public?static?Date?cur;//每局結束時間

public?static?chessOneStep?LineLeft;//結束端點1

public?static?chessOneStep?LineRight;//結束端點2

public?static?boolean?IsGameOver;//是否只有一方獲勝

public?static?int?ColorOfBackGround[][]=?{{255,?227,?132},{0,255,127},{218,165,32}};//背景顏色

public?static?int?ColorOfWindows[][]=?{{?60,179,113},{245,245,245},{122,122,122}};//背景顏色

public?static?int?WitchMatch;//背景搭配

public?static?String?MusicOfBackGround;//背景音樂

public?static?int?CurrentStep;//記錄當前步數

public?static?int?Rank;//設置難度等級

public?static?boolean?IsSurrender;//判斷是否認輸

public?static?boolean?IsTie;//判斷是否認輸

public?static?String?Message;//輸出提示信息

public?static?Image?IconImage;//?圖標

public?static?Image?blackBoard;//白棋盤

public?static?Image?whiteBoard;//黑棋盤

public?static?Image?blackChess;//?白棋棋子圖片

public?static?Image?whiteChess;//?白棋棋子圖片

public?static?Image?RightPlayer;//白棋棋罐圖片

public?static?Image?LeftPlayer;//白棋玩家頭像圖片

public?static?String?path?=?"src/";//?圖片的保存路徑

4.chessButton類

這個是程序的組件類。定義了各種功能鍵,完善程序功能,營造逼真的人機對戰游戲效果。分為3類:效果。。

(1)、按鈕組件

本程序有5個按鈕,支持和棋、認輸、新游戲、退出、悔棋等。認輸和和棋按鈕終止當前的棋局,給出相應的提示信息;退出按鈕調用系統System.exit(0)的函數正常返回;悔棋按鈕調用后面要介紹的chessList類實現悔棋;新游戲按鈕則刷新當前棋局準備下一輪,要將記錄當前棋局的二維數組全部置0,刷新當前棋局開始時間等。

(2)、單選按鈕組件

游戲界面支持設置個性化界面,包括背景顏色與背景音樂,跟重要的一點是設置難度(簡單、中等、困難)。單選按鈕只能多選一。背景顏色主要是存儲相關顏色搭配方案的RGB顏色,開2維數組,即對應RGB3原色數組的一維數組,然后通過改變WitchMatch全局變量的值來有用戶自己選擇顏色搭配,不同的顏色搭配對應不同的背景音樂表達一致的主題。難度設置主要是改變計算機的下棋算法,不同難度通過Rank判斷進入不同的程序分支,實現不同智能等級的計算機下棋水平。

(3)、文本框

在不同的單選按鈕前添加相應的文本框,提示用戶可以實現的功能。同時我用顏色模擬出顯示當前棋局耗用時間的文本框。

不論按鈕還是單選按鈕都要關聯相應的消息,把相應功能的實現放在消息響應處理函數理。這些主要是實現Java庫提供的消息響應接口里的方法。

5.chessPiece類

主要完成當前棋面的存儲,存儲棋面的數據結構為二維數組int[][]?PositionFlag;然后定義獲取、設置某點以及整個棋面的狀態的方法。

(1)、SetPositionFlag(int?x,?int?y,?int?flag)//設置(x,y)處的狀態為flag

(2)、GetPositionFlag(int?x,?int?y)//獲取(x,y)處的狀態

(3)、SetAllFlag(int?[][]NewFlag)//設置當前整個棋面的狀態為NewFlag

(4)、GetAllFlag()//獲取當前整個棋面的狀態

(5)、DrawChessPiece(Graphics?g)//繪制當前局面的棋子

由于本類比較重要,所以附上了代碼,見源代碼1。

6.chessBoard類

功能為繪制棋盤線。由于圍棋的棋盤比較復雜,橫線、豎線較多,且為了使棋盤美觀,還要自定義窗口邊框、棋盤邊框、對弈雙方邊框等,對線寬、線型也有一定要求。有時要單像素線條,有時要多像素線條。對于多像素線條,我主要用了2種方法。

方法一:

在需要繪制多像素線條處首先繪制一條單像素線,然后根據線寬要求上下平移適當像素達到繪制多像素的目的。這樣的方法適合繪制水平線或豎直線,繪制其他斜率的線條容易造成走樣。在沒有想到比較好的反走樣編程思想后我選擇了調用Java庫中已經封裝好的函數。

方法二:

為了克服方法一繪制非水平或豎直線時造成的走樣,同時也為了更進一步學習Java語言,我猜想肯定會有類似OpenGL中設置線寬的畫刷,于是上網百度找到了相應的畫刷Stroke類。通過Java庫實現繪制不同線寬的直線,達到了反走樣效果。

7.chessOneStep類

這個類是為了配合chessList類實現悔棋以及在計算機下棋算法實現返回有效狀態點而設計的。主要數據成員為

private??int??x,y,weight;//其中x,y表示點坐標,weight表示將棋下到該點獲得的估價值。

主要方法如下:

(1)、GetX()//獲得當前對象的x坐標

(2)、GetY()//獲得當前對象的y坐標

(3)、GetWeight()//獲得當前對象的(x,y)處的估價值

8.chessList類

程序支持悔棋功能,為了實現悔棋,自定義了chessList類。這個類主要通過引入java.util.ArrayList和java.util.List實現集合的數據類型。然后自定義一些方法,如下:

(1)、AddStep(chessOneStep?OneStep)//添加一步棋到List中

(2)、GetSize()//獲得當前List的大小

(3)、ClearList()//清空List

(4)、RemoveLast()//刪去List中的最后元素

由于每次刪除當前List中的最后一個元素,實現后進先出,所以可以模擬棧的功能實現悔棋。

9.myCompare類

由于在計算機下棋的極大極小博弈樹算法中需要對自定義對象chessOneStep按weight進行排序,所以引入了myCompare類,通過實現Comparator接口中的compare方法完成自定義對象排序。

10.chessPanel類

程序的自定義面板類,主要負責完成當前框架內容的顯示。這是一個重要的與框架和圖形顯示密切相關的類。主要數據成員為

private?chessboard?MyChessBoard;//當前顯示棋盤

private?chesspiece?MyChessPiece;//當前顯示整個棋面的狀態

主要方法如下:

(1)、chesspanel(chessboard?MyChessBoard1,?chesspiece?MyChessPiece1)//構造函數,分別用MyChessBoard1和MyChessPiece1初始化MyChessBoard和MyChessPiece

(2)display(chessboard?MyChessBoard1,?chesspiece?MyChessPiece1)//自定義顯示回調函數,調用repaint()完成重新繪制游戲界面

(3)、paintComponent(Graphics?g)//核心方法,調用各種函數完成具體的繪制工作

11.chessPositon類

程序算法核心類,總的功能是控制人和計算機輪流下棋,以及調用chessPanel類中的display(chessboard?,?chesspiece?)方法完成界面的實時刷新。關于chessPositon類,我在此將重點介紹。chessPosition類的主要數據成員如下:

private?static?chessboard?MyChessBoard;//當前顯示棋盤

public?static?chesspiece?MyChessPiece;//當前顯示整個棋面的狀態

private?static?chesspanel?Mychesspanel;////當前顯示面板

public?static?chesslist?MyChessList=new?chesslist();//當前下棋集合,用于悔棋

final?private?static?int?INF?=?(1??30);?//?表示正無窮大的常量,用于極大極小博弈數搜索算法

public?static?boolean?CanGo;//控制當前下棋一方

類的設計集中體現在成員方法的設計上。實現人機對戰,只有語言是遠遠不夠的,還要加入算法,用算法引導計算機下棋。下面介紹該類的方法成員:

(1)、chessposition(chesspanel?,?chessboard?,chesspiece?)?//帶有參數的構造函數

(2)、chessposition()

不帶參數的構造函數

(3)、mouseClicked(MouseEvent?event)

鼠標響應函數,負責人的下棋,根據鼠標點擊的位置轉換得到所在棋盤的相對位置。如果該位置不合法,即超出棋盤有效范圍,點擊無響應;如果該位置上已有棋,彈出消息框給出提示。這二者都要求重新給出下棋位置,即當前鼠標響應無效…直到點擊到棋盤有效區域。

(4)、IsOver(int[][]?Array,int?x,int?y)

判斷當前int[][]Array對應的棋局是否結束,即一方五子連成一條直線。此處有兩種思路,一種對當前棋面上的所有棋子都進行一次判斷,具體為水平方向、豎直方向、與水平線成45度方向、與水平線成135度方向,只要有一個方向五子連成一條直線就說明有一方獲勝,游戲結束;另一種思路為只在當前下棋的4個方向進行判斷,我的程序采用的是第二種,所以IsOver方法除了int[][]Array參數外,還有x,y參數,(x,y)表示當前下棋的坐標點。

(5)display()

通過調用自定義面板類的顯示回調函數用于重新顯示游戲界面,達到每下一步棋及時更新游戲界面的目的。

(6)、GetValue(int?flag,?int?num)

估值函數,根據經驗把棋局分成只有1顆棋相連,2顆棋相連且兩端被封死,2顆棋相連且一端封死另一端活的,2顆棋相連且兩端都是活的,同理3顆棋、4顆棋也各自可分3種情況。不同的情況對應不同的估價值。估價值的設定是決定計算機一方是否智能的一個關鍵因素。

(7)、GetPredictValue(int?flag,?int?num)

對未連成一片但通過再下一顆子就能連成一片的局面進行估值,這在雙方下棋的有限步驟內是能產生重要影響的。如果每局棋僅考慮當前一步,是不可取的。

(8)、Evaluate(int[][]?Array,?int?x,?int?y)

根據棋面具體情況以及預先設定的估值函數,對某個點對應的局面進行評估。由于每次雙方只能下一顆棋,所以可以每次取當前局面的所有點中對應估值最大值點的估值作為整個局面的估值。

(9)、GetGreedNext()

計算機下棋方法1,對應難度等級為簡單,采用貪心思想。每次下棋前在求得最有利點下棋,而是否最有利只是通過一步評估。算法偽碼描述為:

Max取負無窮大

for(行i從0到15)

{

For(列j從0到15)

{

If((i,j)對應的位置無棋)

{

a.假設放上一顆由人控制的棋,求估價值;

b.假設放上一顆由計算機控制的棋,求估價值;

c.取二者中較大值作為(i,j)處的估價值tmp;

d.取tmp與Max較大值賦值給Max.

}

}

}

最終Max對應的點就是當前整個局面中最大的估值點。至于上述為什么要考慮雙方都在該點下棋的情況呢?主要原因為下五子棋是個攻防兼備的過程,不僅要考慮自己對自己最有利,還要考慮對對手最不利,通俗來講就是在自己贏的時候不能讓對手先贏。

(10)、GetSearchNext(int?LookLength)

derectSearch(int?[][]Array,boolean?who,int?deepth)

計算機下棋方法2:直接搜索法,對應難度等級為中等。

每步棋最多有225個不同下法,若采用直接搜索法則對應的孩子節點有225個(在下棋過程中會逐漸減少),即每層有最多225個節點待擴展,這就決定了直接搜索進行不超過2次—主要原因有兩點:

a.采用深度優先搜索需要遞歸,遞歸中狀態過多可能會爆棧,我們知道遞歸是用棧機制來實現的;采用寬度優先搜索又需要存儲為擴展的節點,這對內存容量要求很高。

b.不管深搜還是廣搜,在時間復雜度為O(N^m)的情況下都是不能接受的。其中N為當前棋局的待擴展節點,最大225;m為搜索的深度。

綜上所述,在采用直接搜索法時搜索深度不能太深,嚴格來說是應該控制在2層以內,在計算機運算速度在10^7次每秒的情況下,理論和實驗都表明超過2層就會變得很慢且這種趨勢成指數級增長。

直接搜索算法偽代碼為

GetSearch(boolean?flag,int?deep)

{

如果deep等于0,返回當前棋局估值;

for(行i從0到15)

{

For(列j從0到15)

{

If((i,j)對應的位置無棋)

{

如果輪到計算機下棋,置標志位為2

GetSearch(!flag,deep-1);

如果輪到人下棋,置標志位為1;

GetSearch(!flag,deep-1);

}

}

}

}

(11)、GetMinMaxsearchNext(int?LookLength)

MinMaxsearch(int?[][]Array,boolean?who,?int?deepth)

計算機下棋算法3:極大極小博弈樹法,對應難度等級為困難。五子棋是個博弈游戲,當前在尋找對自己最有利的下棋點時要盡可能保證對對手最不利,這種思想可以用極大極小博弈樹

五子棋java代碼請幫忙加一個悔棋功能,急呀急呀

用棧,把每一步操作都記錄下來。悔棋把最后一步從棧中拿出來,然后還原回去

JAVA高手接招————— 五子棋雙人對弈程序

新建一個chess類,其中代碼如下

package work;

import java.awt.Color;

public class Chess {

public static final int diamter=30;

private int x;//在網格中的索引,0~15

private int y;//在網格中的索引,0~15

private Color color;

public Color getColor() {

return color;

}

public void setColor(Color color) {

this.color = color;

}

public int getX() {

return x;

}

public void setX(int x) {

this.x = x;

}

public int getY() {

return y;

}

public void setY(int y) {

this.y = y;

}

public Chess(int x, int y, Color color) {

super();

this.x = x;

this.y = y;

this.color = color;

}

}

然后在同一個包中新建FirstFrame類。代碼如下

package work;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

//主函數

public class FirstFrame{

public static void main(String[] args) {

FirstFrame1 f=new FirstFrame1();

f.setVisible(true);

}

}

//窗體函數

class FirstFrame1 extends JFrame implements ActionListener{

JMenuBar menuBar;

JMenu sysMenu;

JMenuItem startMenuItem;

JMenuItem backMenuItem;

JMenuItem exitMenuItem;

DrawPanel myPanel=new DrawPanel();

public FirstFrame1(){

super("娃哈哈");

add(myPanel);

menuBar=new JMenuBar();

setJMenuBar(menuBar);

sysMenu=new JMenu("系統(s)");

sysMenu.setMnemonic('s');

menuBar.add(sysMenu);

startMenuItem=new JMenuItem("開始");

backMenuItem=new JMenuItem("悔棋");

exitMenuItem=new JMenuItem("退出");

sysMenu.add(startMenuItem);

sysMenu.add(backMenuItem);

sysMenu.addSeparator();

sysMenu.add(exitMenuItem);

startMenuItem.addActionListener(this);

backMenuItem.addActionListener(this);

exitMenuItem.addActionListener(this);

super.setSize(600,650);

super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void actionPerformed(ActionEvent e){

Object obj=e.getSource();

if(obj==startMenuItem){

myPanel.start(); //DrawPanel p=new DrawPanel();是錯的;

}else if(obj==backMenuItem){

myPanel.back();

}else{

System.exit(0);

}

}

}

最后新建一個DrawPanel類代碼如下

package work;

import java.awt.Color;

import java.awt.Cursor;

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 java.awt.event.MouseMotionListener;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import sun.awt.geom.AreaOp.AddOp;

//畫柄;

class DrawPanel extends JPanel implements MouseListener,MouseMotionListener{

final int MARGIN_TOP=60;

final int MARGIN_LEFT=30;

final int CELL_WIDTH=35;

final int COLS=15;

final int ROWS=15;

Chess[] chessList=new Chess[(COLS+1)*(ROWS+1)];

int chessCount=0;

boolean isBlack=true;

boolean gameOver=false;

public DrawPanel(){ //構造函數

Color color=new Color(200,250,200);

setBackground(color);

this.addMouseMotionListener(this);

this.addMouseListener(this);

}

public void start(){

for(int i=0;ichessList.length;i++)

chessList[i]=null;

isBlack=true;

gameOver=false;

chessCount=0;

repaint();

}

public void back(){

if (chessCount==0||gameOver==true)

return;

chessList[chessCount-1]=null;

chessCount--;

isBlack=!isBlack;

repaint();

}

@Override

protected void paintComponent(Graphics g) { //方法重寫

// TODO Auto-generated method stub

super.paintComponent(g);

for(int i=0;i=ROWS;i++){

g.drawLine(MARGIN_LEFT, MARGIN_TOP+i*CELL_WIDTH, MARGIN_LEFT+COLS*CELL_WIDTH, MARGIN_TOP+i*CELL_WIDTH);

}

for(int i=0;i=COLS;i++){

g.drawLine(MARGIN_LEFT+i*CELL_WIDTH, MARGIN_TOP, MARGIN_LEFT+i*CELL_WIDTH, MARGIN_TOP+ROWS*CELL_WIDTH);

}

for(int i=0;ichessCount;i++){ //繪制棋子數組

Chess chess=chessList[i];

int xPos=MARGIN_LEFT+CELL_WIDTH*chess.getX()-Chess.diamter/2;

int yPos=MARGIN_TOP+CELL_WIDTH*chess.getY()-Chess.diamter/2;

g.setColor(chess.getColor());

g.fillOval(xPos,yPos, Chess.diamter, Chess.diamter);

if(i==chessCount-1){

g.setColor(Color.red);

g.drawRect(xPos, yPos, Chess.diamter, Chess.diamter);

}

}

if(isBlack)

g.setColor(Color.black);

else

g.setColor(Color.white);

g.fillOval(8*CELL_WIDTH, CELL_WIDTH/2,Chess.diamter, Chess.diamter);

String colorStr=isBlack==false?"白方下":"黑方下";

g.setColor(Color.blue);

g.drawString(colorStr+" 第"+chessCount/2+"步", 9*CELL_WIDTH, CELL_WIDTH);

}

public void mouseClicked(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

public void mouseDragged(MouseEvent e) {}

public void mouseMoved(MouseEvent e) {

// TODO Auto-generated method stub

super.setCursor(new Cursor(Cursor.HAND_CURSOR));

int x=(e.getX()-MARGIN_LEFT+Chess.diamter/2)/CELL_WIDTH;

int y=(e.getY()-MARGIN_TOP+Chess.diamter/2)/CELL_WIDTH;

if(x0||y0||xCOLS||yROWS) //超出棋盤邊界

super.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

if(findChess(x,y)) //下過的地方不能再下

super.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

if(gameOver)

super.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

}

public void mousePressed(MouseEvent e) {

int x=(e.getX()-MARGIN_LEFT+Chess.diamter/2)/CELL_WIDTH;

int y=(e.getY()-MARGIN_TOP+Chess.diamter/2)/CELL_WIDTH;

if(x0||y0||xCOLS||yROWS) //超出棋盤邊界

return;

Color color=Color.black;

if(!isBlack) // 變顏色

color=Color.white;

if(findChess(x,y)) //下過的地方不能再下

return;

if(gameOver)

return;

chessList[chessCount]=new Chess(x,y,color);

repaint(); //重繪

isBlack=!isBlack;

chessCount++;

String colorStr=color==Color.white?"白方":"黑方";

int[] count={0,0,0,0};

for(int i=x-1;i=0;i--){

if(findChess(i,y,color)){

count[0]++;

}

else

break;

}

for(int i=x+1;i=COLS;i++){

if(findChess(i,y,color)){

count[0]++;

}

else

break;

}

for(int i=y-1;i=0;i--){

if(findChess(x,i,color)){

count[1]++;

}

else

break;

}

for(int i=y+1;i=ROWS;i++){

if(findChess(x,i,color)){

count[2]++;

}

else

break;

}

for(int i=x+1,j=y+1;i=COLSj=ROWS;i++,j++){

if(findChess(i,j,color)){

count[3]++;

}

else

break;

}

for(int i=x-1,j=y-1;i=0j=0;i--,j--){

if(findChess(i,j,color)){

count[3]++;

}

else

break;

}

for(int i=x+1,j=y-1;i=COLSj=0;i++,j--){

if(findChess(i,j,color)){

count[3]++;

}

else

break;

}

for(int i=x-1,j=y+1;i=0j=ROWS;i--,j++){

if(findChess(i,j,color)){

count[3]++;

}

else

break;

}

for(int i=0;i4;i++){

if(count[i]=4){

gameOver=true;

JOptionPane.showMessageDialog(this, "恭喜"+colorStr+"勝利");

return;

}

}

System.out.println(e.getX()+","+e.getY());

}

public boolean findChess(int x,int y){

for(int i=0;ichessCount;i++){

if(chessList[i].getX()==x chessList[i].getY()==y)

return true;

}

return false;

}

public boolean findChess(int x,int y,Color color){

for(int i=0;ichessCount;i++){

if(chessList[i].getX()==x chessList[i].getY()==y chessList[i].getColor()==color)

return true;

}

return false;

}

}

如果不行就hi我

JAVA五子棋中如何實現重新開始和悔棋的功能

一般來說是不應該像你這樣寫的,應該是把要畫的步驟寫在組件的被覆寫的paintComponent函數里,然后在你響應事件的函數里直接掉用該組件的repaint()函數即可。repaint函數是會去掉該組件的paintComponent()函數的。至于你這么寫為啥不行,這個我也不清楚,據我猜測,repaint函數要做的事情是刷新組件,并且重新去畫一遍,不過這個步驟不一定是立即完成,可能有延遲。總之在你畫那些棋子之后,它又干了一些事情,導致你畫的棋子沒了(但這不會影響到它自己想去畫的東西,即paintComponent()函數里的東西),另外不要忘了,覆寫paintComponent時,第一行要寫,super.paintComponent(g),這是去畫它原來有的東西(比如加上去的按鈕,文本框之類的東西)

例如下面:

12345678910111213141516171819202122232425262728293031323334353637class PanelM extends JPanel{ ImageIcon icon = new ImageIcon("app.gif"); JLabel jl = new JLabel(icon, SwingConstants.CENTER); int x, y; private ListInteger listX = new ArrayListInteger(); private ListInteger listY = new ArrayListInteger(); public PanelM() { add(jl); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent m) { x = m.getX(); y = m.getY(); listX.add(x); listY.add(y); repaint(); } }); } public void paintComponent(Graphics g) { super.paintComponent(g); for (int i = 0; i listX.size(); i++) { g.fillOval(listX.get(i), listY.get(i), 20, 20); } }}

java單機版五子棋添加悔棋模塊

每下一步棋,你就把這一步的信息,(位置,誰下的,黑或者白,封裝成一個對象A) 放入一個棧中,點擊上一步,把棧里的數據拿出了,你獲取到一個A對象,根據A對象,把五子棋的數據,還原,應該是用一個二維數組保存五子棋的數據的吧,然后重繪UI,流程應該是這么做,你需要根據A對象來還原到以前的狀態,所以A對象應該保存能還原到以前狀態的完整的信息。這里的細節需要多想想。然后就是 棧的用法了,理解這種數據結構,以及這里為什么要用棧,多想想就好了

本文題目:五子棋悔棋java代碼,五子棋悔棋java代碼
標題鏈接:http://vcdvsql.cn/article30/dsiegso.html

成都網站建設公司_創新互聯,為您提供靜態網站域名注冊網站維護網站收錄關鍵詞優化企業網站制作

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

營銷型網站建設