基于java的五子棋:
創(chuàng)新互聯(lián)于2013年創(chuàng)立,先為克東等服務(wù)建站,克東等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為克東企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
主要功能代碼如下:
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))||????????????????????????????//空紅紅藍(lán)型
((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;
}
}
運(yùn)行結(jié)果如下圖所示:
樓主要是覺得看的不舒服可以逗銀拷到記事本里看~ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.awt.Color; //這一段import就不說了,下面要用到的就import進(jìn)來 public class wuziqi extends Applet implements ActionListener,MouseListener,MouseMotionListener,ItemListener //繼承Applet表明是個applet,后面的接口必須要實現(xiàn)每個接口的所有方法 { int color_Qizi=0;//旗子的顏色標(biāo)識 0:白子 1:黑子 int intGame_Start=0;//游戲開始標(biāo)志 0未開始 1游戲中 int intGame_Body[][]=new int[16][16]; //設(shè)置棋盤棋子狀態(tài) 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); //不設(shè)布局管理器 addMouseListener(this); //將本類作為鼠標(biāo)事件的接口響應(yīng)鼠標(biāo)動作 add(b1); //將事先定義好的第一個按鈕添加入界面 b1.setBounds(330,50,80,30); //設(shè)置第一個按鈕左上角的位置和大小 b1.addActionListener(this); //將本類作為按鈕事件的接口響應(yīng)按鈕動作 add(b2); //將事先定義好的第二個按鈕添加進(jìn)去 b2.setBounds(330,90,80,30); /設(shè)置第二個按鈕左上角的位置和大小 b2.addActionListener(this); //將本類作為按鈕事件的接口響應(yīng)按鈕動作 ckbHB[0]=new Checkbox("白子先",ckgHB,false); //new一個checkbox ckbHB[0].setBounds(320,20,60,30); //設(shè)置左上角位置和大小 ckbHB[1]=new Checkbox("黑子先",ckgHB,false); //new第二虛檔個checkbox ckbHB[1].setBounds(380,20,60,30); //設(shè)置左上角位置和大小 add(ckbHB[0]); //將第一個checkbox加入界面 add(ckbHB[1]); //將第二個checkbox加入界面 ckbHB[0].addItemListener(this); //將本類作為其事件接口來響應(yīng)選中動作 ckbHB[1].addItemListener(this); //將本類作為其事件接口來響應(yīng)選中動作 add(lblWin); //將標(biāo)簽加入界面 lblWin.setBounds(330,130,80,30); //設(shè)置標(biāo)簽的左上角位置和大小 Game_start_csh(); //調(diào)用游戲初始化 } 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) //鼠標(biāo)單擊時 { Graphics g=getGraphics(); //獲得畫筆 int x1,y1; x1=e.getX(); //單擊處的x坐標(biāo) y1=e.getY(); //單擊處的y坐標(biāo) if (e.getX()20 || e.getX()300 || e.getY()20 || e.getY()300) //在棋盤范圍之外 { return; //則這是不能走棋的,直接返回 } //下面這兩個if和兩個賦值的作用是將x和y坐標(biāo)根據(jù)舍入原則修改成棋盤上格子的坐標(biāo) 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程序裝載的時候會調(diào)用這個繪制的過程 { draw_qipan(g); //畫棋盤 }
package com.test;
import java.util.Scanner;
public class? TestMain{
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
char[] name = { '空', '黑', '白' };
int count = 0;// 計數(shù)器,用來計算該誰下棋了
char[][] qi = new char[16][16];// 把棋盤情況放進(jìn)二維數(shù)組中
while (true) {
int row = 0;// 初始化從控制臺接收到的row和col 的index為0
int col = 0;
if (count == 0) {
print(qi, name[count], row, col);
count++;
}
if (count == 1) {
System.out.println("請黑(@)下棋(i j):");
row = console.nextInt(16)%16;// 從控制臺接收到的row的index
col = console.nextInt(16)%16;// 從控制臺接收到的col的index
String condition = print(qi, name[count], row, col);
if (condition.length() == 1) {// 下棋正確
count++;
}
if (condition.length() == 2) {// 下棋出現(xiàn)錯誤,要下的余肢位置已經(jīng)下過棋子
System.out.println("此位置已經(jīng)有棋子,黑棋重新再下");
}
if (condition.length() == 3) {// 黑棋子贏
System.out.println("黑棋很給力,白棋是浮云,黑棋贏了");
break;
}
if (condition.length() == 4) {
System.out.println("白棋子贏");
break;
}
}
if (count == 2) {
System.out.println("請白(*)下棋(i j):");
row = console.nextInt(16);
col = console.nextInt(16);
String condition = print(qi, name[count], row, col);
if (condition.length() == 1) {// 打印兆罩棋盤
count--;
}
if (condition.length() == 2) {// 下棋出現(xiàn)錯誤,要下的位置已經(jīng)下過棋子
System.out.println("此位置已經(jīng)有棋子,白棋重新再下");
}
if (condition.length() == 3) {
System.out.println("黑棋子贏");
break;
}
if (condition.length() == 4) {
System.out.println("白棋很給力,黑棋是浮云,白棋贏了");
break;
}
}
}
}
/**
* @param qi是裝棋盤情況
* @param name輪到誰下棋了
*? ? ? ? ? ? row 行號 col 列號 return true-下棋正確,false-下棋錯誤,重新下
*/
public static String print(char[][] qi, char name, int row, int col) {
String b = "無";//族毀鬧 初始化下棋是正確的
String str = "0123456789111111";
// 打印下第一行
System.out.print(" ");
for (int i = 0; i str.length(); i++) {
System.out.print(str.charAt(i));
}
System.out.println();
for (int i = 0; i qi.length; i++) {
System.out.print(str.charAt(i));
for (int j = 0; j qi[i].length; j++) {
if (name == '空') { // 還沒開始下棋時,棋盤默認(rèn)顯示是'.'
qi[i][j] = '.';
}
if ((name == '黑') (i == row) (j == col)) {
if (qi[i][j] != '.') {// 下的位置已經(jīng)有棋子了,返回false
b = "有棋";
} else {
qi[i][j] = '@';// 黑方下棋后的位置顯示是'@'
if (win(qi, '@', row, col)) {
b = "黑棋子";
}
}
}
if ((name == '白') (i == row) (j == col)) {
if (qi[i][j] != '.') {
b = "有棋";
} else {
qi[i][j] = '*';// 黑方下棋后的位置顯示是'*'
if (win(qi, '*', row, col)) {
b = "白棋子贏";
}
}
}
System.out.print(qi[i][j]);
}
System.out.println();
}
return b;
}
public static boolean win(char[][] qi, char ch, int row, int col) {
boolean b = false;
boolean ns = n2s(qi, ch, row, col);
boolean we = w2e(qi, ch, row, col);
boolean wnes = wn2es(qi, ch, row, col);
boolean enws = en2ws(qi, ch, row, col);
b = ns || we || wnes || enws;
return b;
}
/* 從北到南方向 */
public static boolean n2s(char[][] qi, char ch, int row, int col) {
boolean win = false;
int north = 0;// begin
int south = 0;// end
while (qi[row][col] == ch) {
row--;
if (row 0) {
break;
}
}
row++;// row此時要++,不然等到下面的while(..)中的qi[row][col]永不會==ch
north = row;
while (qi[row][col] == ch) {
row++;
if (row 15) {
break;
}
}
south = row;
if ((south - north) = 5) {
win = true;
}
return win;
}
/* 從西到東方向 */
public static boolean w2e(char[][] qi, char ch, int row, int col) {
boolean win = false;
int west = 0;// begin
int east = 0;// end
while (qi[row][col] == ch) {
col--;
if (col 0) {
break;
}
}
col++;// col此時要++,不然等到下面的while(..)中的qi[row][col]永不會==ch
west = col;
while (qi[row][col] == ch) {
col++;
if (col 15) {
break;
}
}
east = col;
if ((east - west) = 5) {
win = true;
}
return win;
}
/* 從西北到東南方向 */
public static boolean wn2es(char[][] qi, char ch, int row, int col) {
boolean win = false;
int wn = 0;// begin
int es = 0;// end
while (qi[row][col] == ch) {
row--;
col--;
if (row 0 || col 0) {
break;
}
}
row++;// row和col此時要++,不然等到下面的while(..)中的qi[row][col]永不會==ch
col++;
wn = row;
while (qi[row][col] == ch) {
row++;
col++;
if (row 15 || col 15) {
break;
}
}
es = row;
if ((es - wn) = 5) {
win = true;
}
return win;
}
/* 從東北到西南方向 */
public static boolean en2ws(char[][] qi, char ch, int row, int col) {
boolean win = false;
int en = 0;// begin
int ws = 0;// end
while (qi[row][col] == ch) {
row--;
col--;
if (row 0 || col 0) {
break;
}
}
row++;// row和col此時要++,不然等到下面的while(..)中的qi[row][col]永不會==ch
col++;
en = row;
while (qi[row][col] == ch) {
row++;
col++;
if (row 15 || col 15) {
break;
}
}
ws = row;
if ((ws - en) = 5) {
win = true;
}
return win;
}
}
以下是現(xiàn)寫的 實現(xiàn)了兩人對戰(zhàn) 自己復(fù)制后運(yùn)行陵培源把 沒什么難度 類名 Games
import java.util.Scanner;
public class Games {
private String board[][];
private static int SIZE = 17;
private static String roles = "A玩家";
//初始化數(shù)組
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] = "╋";
// }
尺態(tài) }
}
}
//輸出棋盤
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) {
// 從數(shù)組挨個查找找到某個類型的棋子就從該棋子位置向右,向下,斜向右下 各查找5連續(xù)的位置看是否為5個相同的
int xlabel;// 記錄第一次找到某個棋子的x坐標(biāo)
int ylabel;// 記錄第一次找到某個棋子的y坐標(biāo)
// ●○╋
// 判斷人是否贏了
for (int i = 0; i SIZE; i++) {
for (int j = 0; j SIZE; j++) {
if (board[i][j].equals("○")) {
xlabel = i;
ylabel = j;
// 橫向找 x坐標(biāo)不變 y坐標(biāo)以此加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坐標(biāo)不變 y坐標(biāo)以此加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坐標(biāo),必須在0-15范圍內(nèi),xy以空格隔開,輸入16 16結(jié)束程序");
int x = sc.nextInt();
int y = sc.nextInt();
if (x == SIZE y == SIZE) {
System.out.println("程序結(jié)束");
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("此處已經(jīng)有棋子,從新輸入");
continue;
}
if(roles.equals("A玩家")){
roles = "B玩家";
}else if(roles.equals("B玩家")){
roles = "A玩家";
}
}
}
}