Caro Người Máy

Embed Size (px)

Citation preview

  • 8/19/2019 Caro Người Máy

    1/17

     Đầu tiên, mở eclipse và tạo một Java project tên là Gomoku

    Lần lượt tạo các ile !!omputer"java, !Go#an"java, !$iece"java, G$os"java và Gomoku"java v%i nội &un' n(ư sau)

    CComputer.java

    import java.applet.*;

    import java.awt.*;

    public class CComputer {

      CGoban Goban;

    http://www.oracle.com/technetwork/java/http://www.oracle.com/technetwork/java/http://www.oracle.com/technetwork/java/http://en.wikipedia.org/wiki/Gomokuhttp://en.wikipedia.org/wiki/Gomokuhttp://en.wikipedia.org/wiki/Gomokuhttp://www.oracle.com/technetwork/java/

  • 8/19/2019 Caro Người Máy

    2/17

      static fnal int EMPTY = ;

      static fnal int !"#TE = $;

      static fnal int %&'C( = );

      public boolean pace;

      public int core;

      int num+ecursion;  static fnal int M',+EC-+#/ = );

      static fnal int T"#/(0( = ;

      static fnal int T"#/(01#&&E2 = $;

      static fnal int T"#/(0!#/ = );

      static fnal int /T3 = ;

      static fnal int (3 = $;

      static fnal int #&&0C"-+E/= );

      static fnal int #&&044 = 4;

      static fnal int #&&055 = 5;

      static fnal int #&&0/T = 3;

    int *+-.int */-.static inal int *01$ 2 3.static inal int *01$45G67 2 8.static inal int *045G67 2 9.static inal int *0*:;.static inal int *0*:;- 2 3. */>- 2 8.*+A- 2 I8. */A- 2 8.*+B- 2 I8. */B- 2 3.*+C- 2 I8. */C- 2 I8.

    pu#lic int 7(inkDint color ,G$os KestE Fint +,/.int Kestcore 2 3.int eKestcore 2 3.

    int eKestM 2 3.int eKestN 2 3.int ecolor 2 Dcolor22;657?EO KLP!Q ) ;657?.Kest"+ 2 3.Kest"/ 2 3.orD+23.+R!Go#an"K:P4*5S?.+TTEForD/23./R!Go#an"K:P4*5S?./TTEFiD Go#an"$ieces+-/-"tate 22 !$iece"?U$7N VV Go#an"Prea+-/- W 3 EFGo#an"$utDcolor,+,/E.iDGetcoreDcolor,+,/E 22 5LL0

  • 8/19/2019 Caro Người Máy

    3/17

    Kestcore 2 core.Kest"+ 2 +.Kest"/ 2 /.Go#an"4emoveD+,/E.Go#an"$utDecolor,+,/E.

    iDGetcoreDecolor,+,/E 22 5LL0

  • 8/19/2019 Caro Người Máy

    4/17

    Go#an"4emoveD+,/E.

    int $erspective5terDint color,int ecolorE Fint score 2 3.int num 2 3.int +,/,+9,/9.orD+23.+R!Go#an"K:P4*5S?.+TTEForD/23./R!Go#an"K:P4*5S?./TTEFiDGo#an"$ieces+-/-"tate 22 !$iece"?U$7N VV Go#an"Prea+-/- W 3 EFGo#an"$utDecolor,+,/E.orD+923.+9R!Go#an"K:P4*5S?.+9TTEForD/923./9R!Go#an"K:P4*5S?./9TTEFiDGo#an"$ieces+9-/9-"tate 22 !$iece"?U$7N VV Go#an"Prea+9-/9- W 3 EFGo#an"$utDcolor,+9,/9E.GetcoreDcolor,+9,/9E.iDcore W 93EFscore T2 GetGlo#alcoreDcolorE I GetGlo#alcoreDecolorE.

    numTT.Go#an"4emoveD+9,/9E.Go#an"4emoveD+,/E.iDnum 22 3Ereturn 3.return score X num.inal int !0;5< 2 83333333.

    inal int !0;5open23.int c(eck>close23.int c(eckA 2 3.int c(ecklon'23.int num.#oolean space8.core 2 3.orD&82*01$,&92*0*:;

  • 8/19/2019 Caro Người Máy

    5/17

    else iD space8 ZZ pace Ec(eck=closeTT.#reak.case >)iD space8 VV pace Ec(eck>openTT.else i Dspace8 ZZ pace E

    c(eck>closeTT.#reak.case A)c(eckATT.#reak.&eault)iD num W A Ec(ecklon'TT.#reak.iD color 22 !$iece"KLP!Q VV Go#an"QinjiteEFiDc(eck=open W2 9Ereturn 5LL0==.iDc(eck>open T c(eck>close W2 9E

    return 5LL0>>.iDc(ecklon' W 3Ereturn 5LL0!6:14?23.int c(ecklon'23.int num.

    http://en.wikipedia.org/wiki/Relational_operatorhttp://en.wikipedia.org/wiki/Relational_operatorhttp://en.wikipedia.org/wiki/Relational_operatorhttp://en.wikipedia.org/wiki/Relational_operator

  • 8/19/2019 Caro Người Máy

    6/17

    #oolean space8.orD&82*01$,&92*0*:;>TT.iD num W A Ec(ecklon'TT.iDc(eck== W2 9Ereturn 5LL0==.iDc(eck>> W2 9Ereturn 5LL0>>.iDc(ecklon' W 3Ereturn 5LL0!6:14?open,num>close,numA.pu#lic int GetGlo#alcoreDint colorE Fint +,/.#oolean spacec(eck.num9open23.num=open23.num=close23.num>open23.num>close23.numA23.orD+23. + R !Go#an"K:P4*5S?. +TTEF

  • 8/19/2019 Caro Người Máy

    7/17

    /23. spacec(eck2alse.&o FiDGo#an"$ieces+-/-"tate 22 ?U$7NE Fspacec(eck2true./TT. else F/ T2 GetGlo#aleYuenceDcolor,+,/,*0*:;

  • 8/19/2019 Caro Người Máy

    8/17

    int score 2 numA [ 83333 T num>open [ A333 T num=open [ A33T num=close [ 83 T num9open [ 93 T 8.iDnum>close W2 9Escore T2 >333.elsescore T2 num>close [ >33.return score.

    int GetGlo#aleYuenceDint color,int +,int /,int &irection,#oolean spacec(eckE Fint num 2 GeteYuenceDcolor,+,/,&irectionE.iDnum223Ereturn 8.iDnum229EFiDpace VV spacec(eckEnum9openTT. else iDnum22=E FiDpace VV spacec(eckEnum=openTT.else iDpace ZZ spacec(eckEnum=closeTT. else iDnum22>EFiDpace VV spacec(eckE

    num>openTT.else iDpace ZZ spacec(eckEnum>closeTT. else iDnum W2 AE FnumATT.return num.

    CGoban.java

    import java"applet"[.import java"at"[.pu#lic class !Go#an e+ten&s !anvas F

    static inal int 7:$ 2 83.static inal int L?@7 2 83.static inal int K:P4*5S? 2 8A.static inal int $5?!?5S? 2 93.static inal int UPM$5?!?

  • 8/19/2019 Caro Người Máy

    9/17

     Prea 2 ne intK:P4*5S?-K:P4*5S?-.

    pu#lic voi& 5nitiali]eD!!omputer comE F!omputer 2 com.

    orDint +23. + R K:P4*5S?. +TTEForDint /23. / R K:P4*5S?. /TTEF$ieces+-/-"tate 2 !$iece"?U$7N.

     Prea+-/- 2 3.num$iece 2 3.

    pu#lic voi& $utDint color,int +,int /E FiD$ieces+-/-"tate 22 !$iece"?U$7NE F$ieces+-/-"tate 2 color.num$ieceTT.int +8,+9,/8,/9.+8 2 D+IP4?P5S? R 3E O 3 ) +IP4?P5S? .

    +9 2 D+TP4?P5S? W2 K:P4*5S?E O K:P4*5S?I8 ) +TP4?P5S?./8 2 D/IP4?P5S? R 3E O 3 ) /IP4?P5S? ./9 2 D/TP4?P5S? W2 K:P4*5S?E O K:P4*5S?I8 ) /TP4?P5S?.orD . +8 R2 +9. +8TTEForD/ 2 /8 . / R2 /9. /TTE F

     Prea+8-/-TT.

    pu#lic voi& 4emoveDint +,int /E FiD$ieces+-/-"tate \2 !$iece"?U$7NE F$ieces+-/-"tate 2 !$iece"?U$7N.num$ieceII.

    int +8,+9,/8,/9.+8 2 D+IP4?P5S? R 3E O 3 ) +IP4?P5S? .+9 2 D+TP4?P5S? W2 K:P4*5S?E O K:P4*5S?I8 ) +TP4?P5S?./8 2 D/IP4?P5S? R 3E O 3 ) /IP4?P5S? ./9 2 D/TP4?P5S? W2 K:P4*5S?E O K:P4*5S?I8 ) /TP4?P5S?.orD . +8 R2 +9. +8TTEForD / 2 /8. / R2 /9. /TTE F

     Prea+8-/-II.

    pu#lic int !(eckDint color,int +,int /E Fint ret.iD$ieces+-/-"tate \2 !$iece"?U$7NEreturn G!0!P

  • 8/19/2019 Caro Người Máy

    10/17

    iDret 22 !omputer":QAEreturn G!0;5

  • 8/19/2019 Caro Người Máy

    11/17

    File giao diện chính của chương trình Gomo!u.java

    import java"applet"[.import java"at"[.pu#lic class Gomoku e+ten&s Ppplet F$anel KuttonPrea.Kutton Kuttons-.

    La#el Uessa'eLa#el.!(eck#o+ !(eck8,!(eck9.!Go#an Go#an.!!omputer !omputer.int $la/er-.inal int 61UP< 2 8.inal int !:U$17?4 2 9.int !urrent$la/er.inal int ;657? 2 8.inal int KLP!Q 2 9.int Gametate.inal int G0?

  • 8/19/2019 Caro Người Máy

    12/17

    5nitGameDE. else iDe"tar'et instanceo !(eck#o+E FGo#an"aki/omi 2 !(eck8"'ettateDE.Go#an"Qinjite 2 !(eck9"'ettateDE.return true.

    voi& 5nitGameDE FGo#an"5nitiali]eD!omputerE.Go#an"*raDE.repaintDE.Gametate 2 G04?P*N.orDint i23.iR9.iTTEKuttonsi-"ena#leDE.$utUessa'eD^!lick _@irst_ or _econ&_ to start 'ame"^E.

    voi& tartGameDint @irst$la/erE FiDGametate\2G04?P*NEreturn.

    $la/erKLP!Q- 2 @irst$la/er.$la/er;657?- 2 D@irst$la/er22!:U$17?4E O 61UP< ) !:U$17?4.!urrent$la/er 2 KLP!Q.orDint i23.iR9.iTTEKuttonsi-"&isa#leDE.iD@irst$la/er22!:U$17?4EFGo#an"PreaC-C- 2 8.$la/!omputerDE. else FGametate 2 G0$LPN1?4.

    pu#lic #oolean mouse*onD?vent evt,int +,int /E FiDGametate22G0$LPN1?4 VV $la/er!urrent$la/er-2261UP

  • 8/19/2019 Caro Người Máy

    13/17

    return true.pu#lic #oolean mouse1pD?vent evt,int +,int /E FiDGametate 22 G0$LPN!:UEF$la/!omputerDE.

    return true.

    voi& $la/!omputerDE FG$os pos 2 ne G$osDE.$utUessa'eD^!omputer no t(inkin'"""^E.sitc(D!omputer"7(inkD!urrent$la/er,posEE Fcase !!omputer"765

  • 8/19/2019 Caro Người Máy

    14/17

    C67n8 ta c9 t6ể bắt :ầu c6ơi nếu muốn c6ơi lượt :ầu t6< bấm n7t 1irst nếu muốn c6ơi sau

    t6< n6ấn econ

  • 8/19/2019 Caro Người Máy

    15/17

    Kgt hầu c(bi n(

  • 8/19/2019 Caro Người Máy

    16/17

  • 8/19/2019 Caro Người Máy

    17/17

    C67c c>c bạn t6?n6 c@n8