Simple Black and White - My First Program

xiaoxiao2021-03-06  40

/ ** * @Author foas@bbs.xjtu.edu.cn on oct.23,2004 12:00 * * Todo to change the template for this generated Type Comment Go to Window - * Preferences - Java - Code Generation - Cofeng Comments * /

Import java.awt. *; import java.awt.event. *;

Import javax.swing. *;

Public class first extends jframe {

public First () {DrawPane drawPane = new DrawPane (getBackground ()); Container pane = getContentPane (); pane.add (drawPane, BorderLayout.CENTER); setSize (600, 580); //f.setExtendedState(JFrame.MAXIMIZED_BOTH ); //F.setundecoated (true); //f.setalwaysontop (true); //f.setLocationByPlatform (true);

SetLocation (208, 104); setDefaultcloseOperation (jframe.exit_on_close); //setbackground (color.pink); setResizable (false); setvisible (true);}

Public static void main (string [] args) {new first ();}}

Class Drawpane Extends JPanel {Private INSETS INSETS

Final static int black = -1;

Final static int white = 1;

PRIVATE POINT POINT = NULL;

Private Int Colorstatus;

Private int [] [] status = null;

PRIVATE INT COUNT;

PRIVATE INT CHESSNUM;

Private JtextArea Infotext = NULL;

Private int [] [] statusw = null;

Private int discount = 1;

Private int whitecount = 0;

Private int blackcount = 0; private color bgcolor = null; Public Drawpane (Color C) {

initComponent (); initChess (); bgColor = c; addMouseListener (new MouseAdapter () {public void mouseClicked (MouseEvent e) {response (e.getPoint ());}}); setFocusable (true); addKeyListener (new myKeyListener ( ));

Public void PainTComponent (GRAPHICS G) {Super.PaintComponent (G); if (insets == null) {INSETS = getInsets ();} //super.paintComponents (GRAPHICS2D G2D = (Graphics2D) g; g2d.setcolor (Color.range); G2D.Fill3DRECT (50, 50, 400, 400, true); g2d.setcolor; for (int i = 0; i <8; i ) {for (int J = 0 J <8; J ) {G2D.DrawString (i "", 50 * i 75, 45); g2d.drawstring (j "", 40, 50 * j 75); g2d.drawRect (50 50 * i, 50 50 * j, 50, 50); if (status [i] [j] == black) {g2d.setcolor (color.black); g2d.filloval (i * 50 53, j * 50 53, 44, 44);} IF (status [i] [j] == white) {g2d.setcolor (color.white); G2D.Filloval (i * 50 53, j * 50 53, 44 , 44);} IF (status [i] [j]> 1) {// g2d.setcolor (color.blue); // g2d.drawstring ("" (status [i] - 1), i * 50 75, // j * 50 75); g2d.drawstring (" X ", I * 50 75, J * 50 75);} g2d.setcolor (color.black); g2d.filloval (200, 470, 30, 30);

g2d.setcolor (color.white); g2d.filloval (300, 470, 30, 30);

G2d.SetColor; G2D.FillRect (240, 470, 60, 30); g2d.fillRect (340, 470, 60); g2d.setcolor; g2d.drawstring (" blackcount , 240, 490); g2d.drawstring (" Whitecount, 340, 490);}} // jug (colorstatus);

public void initComponent () {infoText = new JTextArea (); setLayout (new BorderLayout ()); infoText.setEditable (false); //infoText.setBackground(Color.PINK); infoText.setForeground (Color.BLACK); JScrollPane jsp = New Jscrollpane (Infotext); JPanel InfoPanel = New JPanel (New BorderLayout ()); //infopanel.setBackground; InfoPanel.Add (JSP, BorderLayout.center);

InfoPanel.add (New Jlabel ("), BorderLayout.North); InfoPanel.Add (New Jlabel (" "), BorderLayout.South;

Add (InfoPanel, BorderLayout.east);

}

Public voidin = 4; count = 0; colorStatus = black; status = new int [8] [8]; status [3] [3] = black; status [4] [4] = black; status [3] [4] = White; status [4] [3] = white; statusw = new int [8] [8]; for (int i = 0; i <8; i ) {for (int J = 0 ; J <8; J ) {statusw [i] [j] = 2;}}

For (int i = 0; i <4; i ) {statusw [i 2] [0] = 3; statusw [i 2] [1] = 1; StatusW [i 2] [7] = 3; STATUSW [i 2] [6] = 1; statusw [0] [i 2] = 3; Statusw [1] [i 2] = 1; StatusW [7] [i 2] = 3; Statusw [ 6] [i 2] = 1;} for (int i = 0; i <2; i ) {for (int J = 0; j <2; j ) {statusw [i] [j] = 0; statusw [7 - i] [j] = 0; Statusw [i] [7 - j] = 0; Statusw [7 - i] [7 - j] = 0;}} statusw [0] [0] = 4; statusw [0] [7] = 4; STATUSW [7] [0] = 4; statusw [7] [7] = 4; jug (colorStatus); string msg = "f1: Level 1 / N f2: Level 2 / N F3: Restart "; Infotext.Settext (msg " / n "); repaint ();}

Public void response (POINT P) {Boolean Interrupt = false; point = setpoint (p); if (Point! = null) {INT x = Point.x / 50 - 1; int y = point.y / 50 - 1;

IF (Check (X, Y, Colorstatus> 0) {

Manulchess (x, y, black); if (level == 1) {AutoChess (White);} else {AutoChess (White, 2);} if (blpass ()) {if (! BLPass ()) {repaint } Else {interrupt = true;}}} (interrupt)) {INITCHESS ();}}

}

}

Public Point SetPoint (POINT P) {IF (PX <50 || PX> 450 || PY <50 || PY> 450) {Return Null;} else {PX = (PX / 50) * 50; PY = (PY / 50) * 50; Return P;}} public boolean change (int I, int J, int STA) {

Boolean flag = false;

// north if (j! = 0) {if (sta! = status [i] [j - 1] && 1 == math.abs (status [i] [j - 1])) {INT M = J - 1; while (status [i] [j - 1] == status [i] [m] && m> 0) {m-; // system.out.println (m);} if (status [i] [M] == STA) {Flag = true; for (int N = j - 1; n> m; n--) {status [i] [n] = sta; // system.out.println ("[" " i ", " n "] "// status [i] [n]);}}}}

// South IF (j! = 7) {if (sta! = status [i] [j 1] && 1 == math.abs (status [i] [j 1]) {INT m = j 1; while (status [i] [j 1] == status [i] [m] && m ; // system.out.println (m);} if (status [i] [m ] == STA) {Flag = true; for (int N = j 1; n

// West if (i! = 0) {if (sta! = status [i - 1] [j] && 1 == math.abs (status [i - 1] [j])) {INT m = i - 1; while (status [i - 1] [j] == status [m] [j] && m> 0) {m-; // system.out.println (m);} if (status [m] [J] == STA) {FLAG = true; for (int N = i - 1; n> m; n--) {status [n] [j] = sta; // system.out.println ("[" " n ", " J "] "// status [n] [j]);}}}}}

// East if (i! = 7) {IF (sta! = status [i 1] [j] && 1 == math.abs (status [i 1] [j])) {int m = i 1; while (status [i 1] [j] == status [m] [j] && m ; // system.out.println (m);} if (status [m] [j ] == STA) {Flag = true; for (int N = i 1; n

// northwest if (j! = 0 && i! = 0) {if (sta! = status [i - 1] [j - 1] && 1 == math.abs (status [i - 1] [j - 1 ])) {INT M = I - 1; INT N = J - 1; While (status [i - 1] [j - 1] == status [m] [n] && m> 0 && n> 0) { M -; n-; // system.out.println ("m =" m "n =" n);}} (status [m] [n] == STA) {flag = true; For (int x = i - 1, y = j - 1; x> m; x--, y -) {// for (int y = j - 1; y> n; y -) {status [ x] [y] = sta; // system.out.println ("[" x "," "]" // status [x] [y]); //}}}}}

// SoutHeast IF (j! = 7 && i! = 7) {if (sta! = status [i 1] [j 1] && 1 == math.abs (status [i 1] [J 1 ])) {INT M = I 1; INT N = J 1; While (status [i 1] [j 1] == status [m] [n] && m <7 && n <7) { M ; N ; // system.out.println ("m =" m "n =" n);}} (status [m] [n] == STA) {flag = true; for (int X) = i 1, y = j 1; x

// Northeast if (j! = 0 && i! = 7) {if (sta! = status [i 1] [j - 1] && 1 == math.abs (status [i 1] [J - 1 ])) {INT M = I 1; INT N = J - 1; While (status [i 1] [j - 1] == status [m] [n] && m <7 && n> 0) { M ; n--; // system.out.println ("m =" m "n =" n);} f (status [m] [n] == STA) {flag = true; for INT x = i 1, y = j - 1; x n; y -) {status [x] [Y ] = sta; // system.out.println ("[" x "," Y "]" // status [x] [y]); //}}}}}

// southwest if (j! = 7 && i! = 0) {if (sta! = status [i - 1] [j 1] && 1 == math.abs (status [i - 1] [j 1 ])) {INT M = I - 1; INT N = J 1; While (status [i - 1] [j 1] == status [m] [n] && m> 0 && n <7) { M -; N ; // system.out.println ("m =" m "n =" n);} f (status [m] [n] == STA) {flag = true; for ( INT x = i - 1, y = j 1; x> m; x--, y ) {// for (int y = j 1; y

Status [x] [y] = sta; // system.out.println ("[" x "," y "]" // status [x] [y]); //}

}}}} RETURN FLAG;

}

Public int Check (INT I, INT J, INT STA) {

INT flag = 0; if (Math.Abs ​​(status [i] [j]) == 1) {return 0;}

// north if (j! = 0) {if (sta! = status [i] [j - 1] && 1 == math.abs (status [i] [j - 1])) {INT M = J - 1; while (status [i] [j - 1] == status [i] [m] && m> 0) {m-; // system.out.println (m);} if (status [i] [M] == STA) {FLAG = flag (j - 1 - m);}}} // South IF (j! = 7) {IF (STA! = status [i] [j 1] && 1 == math.abs (status [i] [j 1])) {INT M = J 1; while (status [i] [j 1] == status [i] [m] && m] && m <7) {M ; // system.out.println (m);} if (status [i] [m] == STA) {Flag = flag (m - j - 1);}

}

// West if (i! = 0) {if (sta! = status [i - 1] [j] && 1 == math.abs (status [i - 1] [j])) {INT m = i - 1; while (status [i - 1] [j] == status [m] [j] && m> 0) {m-; // system.out.println (m);} if (status [m] [J] == STA) {FLAG = flag (i - 1 - m);

}}}

// East if (i! = 7) {IF (sta! = status [i 1] [j] && 1 == math.abs (status [i 1] [j])) {int m = i 1; while (status [i 1] [j] == status [m] [j] && m ; // system.out.println (m);} if (status [m] [j ] == STA) {FLAG = flag (M - I - 1);}

}

// northwest if (j! = 0 && i! = 0) {if (sta! = status [i - 1] [j - 1] && 1 == math.abs (status [i - 1] [j - 1 ])) {INT M = I - 1; INT N = J - 1; While (status [i - 1] [j - 1] == status [m] [n] && m> 0 && n> 0) { m - n--; // system.out.println ("m =" m "n =" n);} if (status [m] [n] == STA) {Flag = flag (i - 1 - m);

}}}

// SoutHeast IF (j! = 7 && i! = 7) {if (sta! = status [i 1] [j 1] && 1 == math.abs (status [i 1] [J 1 ])) {INT M = I 1; INT N = J 1; While (status [i 1] [j 1] == status [m] [n] && m <7 && n <7) { M ; n ; // system.out.println ("m =" m "n =" n);} f (status [m] [n] == STA) {flag = flag (m - i - 1);} } }

// Northeast if (j! = 0 && i! = 7) {if (sta! = status [i 1] [j - 1] && 1 == math.abs (status [i 1] [J - 1 ])) {INT M = I 1; INT N = J - 1; While (status [i 1] [j - 1] == status [m] [n] && m <7 && n> 0) { M ; n-; // system.out.println ("m =" m "n =" n);}} (status [m] [n] == STA) {flag = flag (j - 1 - n);

}}}

// southwest if (j! = 7 && i! = 0) {if (sta! = status [i - 1] [j 1] && 1 == math.abs (status [i - 1] [j 1 ])) {INT M = I - 1; INT N = J 1; While (status [i - 1] [j 1] == status [m] [n] && m> 0 && n <7) { m -; n ; // system.out.println ("m =" m "n =" n);}} (status [m] [n] == STA) {flag = flag (i - 1 - m);}}} RETURN FLAG;

}

Public void release () {for (int i = 0; i <8; i ) {for (int J = 0; j <8; j ) {if (status [i] [j]> 1) {status [i ] [j] = 0;}}}}

Public Boolean Jug (Int Color) {// HashTable Table = New HashTable (); INT FLAG = 0; ISPAS = 0; for (INT I = 0; I <8; I ) {for (INT J = 0; J <8; J ) {IF (status [i] [j] == 0) {Flag = Check (i, j, color); {if (flag! = 0) {status [i] [j] = flag 1; ISPASS ;}}

} else {FLAG = 0;

}}} (Ispass> 0) {return true;} else {return false;}

}

Public Boolean Blovern (Boolean Interrupt) {String MSG = ""; WhiteCount = 0; BlackCount = 0; for (int i = 0; i <8; i ) {for (int J = 0; J <8; J ) { IF (status [i] [j] == black) {blackcount ;} if (status [i] [j] == white) {WhiteCount ;}}} f ((WhiteCount blackcount) == 64 || Blackcount = = 0 || WhiteCount == 0 || interrupt) {if (WhiteCount> Blackcount) {msg = "White Fang Sheng! White: Black =" WhiteCount ":" Blackcount;} else {msg = "Black Sheng Black: White " Blackcount ": " Whitecount;} JOPTIONPANE.SHOWCONFIRMDIALOG (THIS, MSG," Game over ", joptionpane.closed_option;} else {returnaf false;}

}

public boolean blPass () {if (chessNum> = 64) {return false; (! jug (colorStatus))} if {JOptionPane.showMessageDialog (null, "pass"); colorStatus = -colorStatus; count ; infoText.append (count ": - / n"); return true;} else {return false;}}

Public Void Manulchess (INT X, INT Y, INT Color) {if (ColorStatus == Color) {release (); if (status [x] [y] == 0) {status [x] [y] = colorstatus;

Change (x, y, colorStatus); repaint (); count ; chessnum ; string value = " x " * " y; Infotext.Append (count ": value "/ n"); Colorstatus = -colorstatus;}}}}} {if (colorStatus == color) {int flag = 0; int x = 0; int y = 0; for (int i = 0; i <8; i ) ) {For (int J = 7; j> = 0; j--) {int Temp = Check (i, j, colorStatus); if (Temp> Flag) {flag = Temp; x = i; y = j; }}}} {= {I (status [x] [y] == 0) {status [x] [y] = color;

Change (x, y, colorStatus); repaint (); colorStatus = -colorstatus; count ; chessnum ; string value = " x " * " y; infintxt.append (count ": " value " / n ");} else {colorStatus = -colorstatus;}}}

Public void autochess (int color, int discount) {if (colorStatus == color && level == 2) {int flag = -1; int x = -1; int y = -1; for (int i = 0; i <8; i ) {for (int J = 7; j> = 0; j -) {i (status [i] [j] == 0 && statusw [i] [j]> flag) {if (Check (i, j, colorStatus> 0) {flag = statusw [i] [j]; x = i; y = j;}}}}} (flag> = 0 && x> -1 &&& y> -1) {IF (status [x] == 0) {status [x] [y] = color;

Change (x, y, colorStatus); repaint (); colorStatus = -colorstatus; count ; chessnum ; string value = " x " * " y; infintxt.append (count ": " value " / n ");} else {colorStatus = -colorstatus;}}}

class myKeyListener extends KeyAdapter {public void keyPressed (java.awt.event.KeyEvent e) {System.out.println (e.getKeyChar ()); if (e.getKeyCode () == KeyEvent.VK_F5) {initChess ();

转载请注明原文地址:https://www.9cbs.com/read-71685.html

New Post(0)