Java: Othello (Othello)

Source: Internet
Author: User

In fact, this study summary should be written early, but the technical blog does not grasp the place, always feel that the language is not vivid enough, expression also slightly far-fetched, in addition to paste a few lines of code, the middle of the technical points revealed not too much, after one months of degradation, this article was to be shot out of the experience and I took out to fry a fried leftovers. (source code is shared below)

The teacher once said, learning is really not the more progress, now think back, before the knowledge of the familiar now is really slowly unfamiliar up, we learned how to make Java Gobang, after repeated hammer mill, I was a hero, But before because of lack of self-confidence has been looking for excuses not to find their own problems, as the teacher said, we see more and more people around the cattle, on the contrary, we have become more lazy.

This problem is my girlfriend left me, I went to the company to learn less than one months Java time, on the big words do not slander promised her: "Winter vacation I write a game for you to play." "Beginners are like this, we grow in the process, the strangest thing is, obviously than before, more and more timid, only a child is not afraid of anything." As a novice, just wrote a landing interface to say that they want to write a QQ program, the road long its repair far XI, know this matter to preach (PS: This sentence is from two poems OH).

In the beginning also did not have any idea, looked the book to have a chapter to introduce repeatedly to see, in the mind, girls generally prefer to play repeatedly look (I used to play with her in the Internet café two hours of repeatedly watching), the heart is not through the music, but in that day playing Gobang, Suddenly think of a very small time in the mother's mobile phone play a game, vaguely remember the name seems to be, yes, is black and white chess, Baidu above a search, there is really, the following encyclopedia.

Black and white chess, also called Othello Chess, anti-chess (Reversi), Apple Chess or flip chess, to borrow Shakespeare's famous plays Othello for its renaming.

The way:

Put your own color of the pieces on the space, when the top or bottom or oblique direction of a piece of their own, sandwiched in the middle of the pieces will flip into their own pieces, and only can flip the place to the next pawn.

The chessboard is as shown in the picture:

At first, I think this game and Gobang should not be much worse, anyway are black and white, cooked do not know, the same packaging, but is not the same as the core, like you go to the store to buy a yolk pie, packaging above clearly said two, the result opened a look, but there are four. OK, the gossip is not so much, the following share the code for everyone to fight, because is specially prepares for the girlfriend, therefore the interface also has made the very big reform Oh, the code is a bit long, explained I as far as possible adds the detail, in the middle was commented out the content is the original modelling time uses, everybody has the interest slowly to see, if has the mistake , also please advise:

The following are the interface classes:

Java code

Import java.awt.Dimension;  
Import Java.awt.Font;  
Import Java.awt.Graphics;  
Import Java.awt.GridLayout;  
Import Java.awt.Image;  
Import java.awt.event.ActionEvent;  
      
Import Java.awt.event.ActionListener;  
Import Javax.swing.ImageIcon;  
Import Javax.swing.JButton;  
Import Javax.swing.JFrame;  
Import Javax.swing.JLabel;  
Import Javax.swing.JPanel;  
        @SuppressWarnings ("Serial") public class Chess extends JFrame {public static void main (String args[]) {  
        Chess ch=new Chess ();  
    Ch.showframe (); //Show game Interface public void Showframe () {//--------------------game Interface----------------------Jpane  
        L gamejp=new JPanel ();  
        --------------------Component Interface----------------------JPanel buttonjp=new JPanel ();  
        button icon ImageIcon ali=new imageicon ("image/ali. jpg");  
        ImageIcon taozi=new ImageIcon ("image/peach. jpg");  
        ImageIcon ali1=new ImageIcon ("image/Ali 1.jpg"); ImageIcon TaozI1=new ImageIcon ("image/peach 1.jpg");  
        JButton alibu=new JButton (Ali);  
        JButton taozibu=new JButton (Taozi);  
        Alibu.setrollovericon (ALI1);  
        Taozibu.setrollovericon (TAOZI1);  
        Alibu.setpressedicon (ALI1);  
        Taozibu.setpressedicon (TAOZI1);  
        Dimension di=new Dimension (100,100);  
        Alibu.setpreferredsize (DI);  
        Taozibu.setpreferredsize (DI);  
        -----------------------the current chess player---------------------final JLabel jilu=new JLabel ("Ali");  
        Set Font jilufont=new fonts ("bold", font.bold,30);  
        Jilu.setfont (Jilufont);  
        Used to record the number of Ali and peaches final JLabel alila=new JLabel ("2");  
        Final JLabel taozila=new JLabel ("2");  
        Set the font and size of the label Fonts font=new font ("XXFarEastFont-Arial", font.bold,42);  
        Alila.setfont (font);  
        Taozila.setfont (font); -----------------the way to start again------------------ImageIcon img=new imageicon ("image/restart.jpg");  
        JButton bu=new JButton (IMG);  
        Bu.setpreferredsize (New Dimension (100,40));  
        Buttonjp.add (Jilu);  
        Buttonjp.add (Alibu);  
        Buttonjp.add (Alila);  
        Buttonjp.add (Taozibu);  
        Buttonjp.add (Taozila);  
        Buttonjp.add (BU);  
        This.setlayout (New GridLayout (1,2,600,0));  
        This.add (GAMEJP);  
        This.add (BUTTONJP);  
        This.settitle ("Ali & Peach");  
        This.setsize (1000,650);  
        This.setresizable (FALSE);  
        This.setlocationrelativeto (NULL);  
        This.setdefaultcloseoperation (3);  
        Display window this.setvisible (true);  
        Gets the canvas permission Graphics g=this.getgraphics ();  
        Chess[3][3]=1;  
        Chess[4][4]=1;  
        Chess[3][4]=-1;  
        Chess[4][3]=-1;  
        Chesslistener lis=new Chesslistener (G,this,jilu,alila,taozila);  
        This.addmouselistener (LIS); Button Listener----------------restart ActionListener bulis=new ActionListener (){public void actionperformed (ActionEvent e) {clear ();  
                The redrawing method repaint ();  
                Draw the initial four pictures chess[3][3]=1;  
                Chess[4][4]=1;  
                Chess[3][4]=-1;  
                Chess[4][3]=-1;  
                Change state to initial state chesslistener.stata=1;  
                Jilu.settext ("Ali under");  
                Alila.settext ("2");  
            Taozila.settext ("2");  
        }  
        }; If you click Ali Ali under ActionListener alilis=new ActionListener () {public void actionperformed (ActionEvent E  
                ) {chesslistener.stata=1;  
            Jilu.settext ("Ali under");  
        }  
        };  If click Peach then peach under ActionListener taozilis=new ActionListener () {public void actionperformed (ActionEvent  
                e) {chesslistener.stata=-1;  
 Jilu.settext ("Under the Peach");           }  
        };  
        Alibu.addactionlistener (Alilis);  
        Taozibu.addactionlistener (Taozilis);  
    Bu.addactionlistener (Bulis);  
            /** * The method of emptying the chessboard */public void Clear () {for (int i=0;i<chess.length;i++) {  
            for (int j=0;j<chess[i].length;j++) {chess[i][j]=0;   
        }}/** * Overrides the parent class's Paint method/public void Paint (Graphics g) {super.paint (g);  
        Image back=new ImageIcon ("image/chessboard. jpg"). GetImage ();  
        G.drawimage (back, 10,30,800,600, null);  
Redraw chessboard//dash//for (int i=0;i<rows;i++) {//G.setcolor (Color.Blue);  
G.drawline (x,y+i*size,x+size* (rows-1,y+i*size);  
/////row vertical/for (int j=0;j<cols;j++) {//G.setcolor (Color.Blue);  
G.drawline (x+j*size,y,x+j*size,y+size* (cols-1)); //Draw a pawn for (int i=0;i<rows-1;i++) {for (int j=0;j<cols-1;j++) {int x=x+size/2+size*i;//the horizontal axis of a pawn int y=y+size/2+size*j;//chess ordinate if (chess[i][j]==1) {//Draw black Im  
                    Age b=new ImageIcon ("Image/ali.jpg"). GetImage ();  
                    G.drawimage (b, X-CHESS_SIZE/2, Y-CHESS_SIZE/2, chess_size, chess_size, NULL);  
                G.filloval (x-size/2,y-size/2,chess_size,chess_size); else if (chess[i][j]==-1) {//Draw white Image w=new ImageIcon ("Ima  
                    Ge/taozi.jpg "). GetImage ();  
                    G.drawimage (W, X-CHESS_SIZE/2, Y-CHESS_SIZE/2, chess_size, chess_size, NULL);  
                G.filloval (x-size/2,y-size/2,chess_size,chess_size);   }}} public final static int x=79;   Checkerboard Initial Point horizontal axis public final static int y=83; Checkerboard Initial point ordinate public final static int rows=9; Board row number public final static int cols=9; Checkerboard columns Public final static int size=61;//checkerboard Grid size public final static int chess_size=56;//pawn size public fina The l static int chess[][]=new int[rows-1][cols-1];//defines an array of 8*8 to hold the pieces}

The following listening classes, the main methods are in this area:

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.