Cov txheej txheem:

Program Koj Tus Kheej 2048 Game W/Java !: 8 Kauj Ruam
Program Koj Tus Kheej 2048 Game W/Java !: 8 Kauj Ruam

Video: Program Koj Tus Kheej 2048 Game W/Java !: 8 Kauj Ruam

Video: Program Koj Tus Kheej 2048 Game W/Java !: 8 Kauj Ruam
Video: txoj kev xav ntawm koj tus kheej 2024, Kaum ib hlis
Anonim

Los ntawm PranP1My (Ua tsis tiav) SiteFollow Ntau los ntawm tus sau:

Arduino CNC Tshuab Los Ntawm Khaws PC Xeem
Arduino CNC Tshuab Los Ntawm Khaws PC Xeem
Arduino CNC Tshuab Los Ntawm Khaws PC Xeem
Arduino CNC Tshuab Los Ntawm Khaws PC Xeem
DIY Niaj Hnub Ntoo Ntoo
DIY Niaj Hnub Ntoo Ntoo
DIY Niaj Hnub Ntoo Ntoo
DIY Niaj Hnub Ntoo Ntoo
Tsis Siv Neeg Qhov Rooj Qhib (vSTEM)
Tsis Siv Neeg Qhov Rooj Qhib (vSTEM)
Tsis Siv Neeg Qhov Rooj Qhib (vSTEM)
Tsis Siv Neeg Qhov Rooj Qhib (vSTEM)

Kuv nyiam qhov kev ua si 2048. Thiab yog li kuv txiav txim siab los ua haujlwm kuv tus kheej version.

Nws zoo ib yam li qhov kev ua si tiag tiag, tab sis kev tsim kho nws tus kheej ua rau kuv muaj kev ywj pheej hloov qhov kuv xav tau txhua lub sijhawm kuv xav tau. Yog tias kuv xav tau 5x5 kev ua si tsis yog siv 4x4, kev hloov pauv yooj yim siv 'Board' tus tsim khoom yuav tso cai rau kuv ua li ntawd. Hais tias kuv xav ua kom qhov kev ua si nyuaj dua, ntxiv cov khoom ntawm txoj haujlwm uas yuav ua rau nws nyuaj tshaj plaws rau cov neeg uas ua ntawv ntau dua li ntawm random. Siv cov txheej txheem yooj yim, kuv tuaj yeem ua qhov ntawd. Thaum kuv yuav tsis npog tag nrho cov kev hloov pauv hauv Cov Lus Qhia no, Kuv npaj yuav ntxiv ntau ntxiv thaum kuv mus.

Txog tam sim no, txawm li cas los xij, peb yuav tsim koj li kev ua si ntawm 2048.

Cia peb pib!

(Ib daim ntawv ceeb toom: Phau ntawv qhia no xav tau kev paub txog nruab nrab ntawm kev ua haujlwm - tshwj xeeb nrog Java)

Kauj ruam 1: Cov ntaub ntawv

Khoom siv
Khoom siv

Koj yuav tsis xav tau ntau rau qhov haujlwm no vim nws tsuas yog kev qhia ua haujlwm.

Cov ntaub ntawv:

  • Laptop
  • Dab noj hnub (lossis ib qho IDE ntawm koj xaiv)

Yog lawm. Qhov ntawd yog nws.

Kauj Ruam 2: Paub Txog Txoj Haujlwm - Pawg Thawj Coj

Kuv muab tag nrho kuv cov lej tso rau GitHub - txheeb xyuas nws ntawm no:

Kuv faib qhov kev ua si mus rau 3 chav kawm: Board, Pobzeb thiab Game.

Pawg Thawj Coj:

Nqe lus piav qhia: Pawg thawj coj saib xyuas cuam tshuam nrog cov gameboard, teeb tsa ntau yam ntawm 'Pobzeb' cov ntsiab lus, tau txais cov qhab nia tam sim no thiab cov pobzeb siab tshaj plaws, thiab tso cov kab hauv kab (kom siv tom qab hauv 'Game'). Feem ntau ntawm cov laj thawj tseem nyob ntawm no, chav kawm muab cov txheej txheem rau kev sib tw 2's thiab 4's ntawm qhov chaw tsis sib xws, txav mus, nqis, sab laug thiab sab xis, thiab qhia rau cov players paub thaum qhov kev ua si tas lawm.

Cov neeg tsim khoom:

/ * Default constructor for the Board - teeb tsa 4x4 matrix */

Pawg Thawj Coj () {…}

/ * Tus neeg tsim khoom rau Pawg Thawj Coj - teeb tsa lub matrix nrog cov kab sib chaws tshwj xeeb loj */

Public Board (int grids) {…}

Txoj kev:

/ * Txoj hauv kev Getter uas rov qab los ntawm lub rooj tsavxwm */

pej xeem Pobzeb getBoard () {…}

/ * Txoj hauv kev Getter uas rov qhab nia */

pej xeem int getScore () {…}

/ * Pom cov pobzeb siab tshaj plaws ntawm lub rooj tsavxwm thiab rov qab los */

pej xeem int getHighTile () {…}

/ * Luam tawm lub rooj tsavxwm mus rau lub console - rau kev ntsuas lub hom phiaj */

pej xeem tsis muaj dab tsi luam tawm () {…}

/ * Rov qab los ntawm lub rooj tsavxwm ua txoj hlua - siv hauv GUI */

public String toString () {…}

/ * Spawns 2 (lossis 4) ntawm qhov chaw khoob ntau lub sijhawm txav mus */

pej xeem tsis muaj dab tsi tshwm sim () {…}

/ * Txheeb xyuas seb lub rooj tsavxwm puas dub tag thiab yog nws yog, nws yuav ua rau cov players rov pib dua */

pej xeem boolean blackOut () {…}

/ * Txheeb xyuas seb qhov kev ua si dhau los - thaum lub rooj tsavxwm dub thiab tsis muaj cov nplais tuaj yeem ua ke */

pej xeem boolean gameOver () {…}

/ * Hu thaum 'w' lossis xub xub raug nias - hu 'verticalMove' rau txhua pobzeb ntawm lub rooj tsavxwm nrog ntsuas 'nce' */

pej xeem tsis muaj dab tsi tshwm sim () {…}

/ * Hu thaum 's' lossis xub xub raug nias - hu 'verticalMove' rau txhua pobzeb ntawm lub rooj tsavxwm nrog qhov ntsuas 'nqis' */pej xeem tsis muaj dab tsi nqis () {…}

/ * Hu thaum 'd' lossis xub xub raug nias - hu 'horizontalMove' rau txhua pobzeb ntawm lub rooj tsavxwm nrog qhov ntsuas 'txoj cai' */pej xeem tsis muaj cai () {…}

/ * Hu thaum 'a' lossis xub xub raug nias - hu 'horizontalMove' rau txhua pobzeb ntawm lub rooj tsavxwm nrog qhov ntsuas 'sab laug' */

pej xeem tsis muaj dab tsi sab laug () {…}

/* Sib piv ob lub pobzeb qhov tseem ceeb ua ke thiab yog lawv zoo ib yam lossis yog ib qho sib npaug rau 0 (pobzeb dawb) - lawv qhov txiaj ntsig tau ntxiv (muab tias cov vuas peb sib piv yog ob lub vuas sib txawv thiab lawv tau txav mus rau qhov kev coj ua uas tsim nyog) - rov ua dua los ntawm kab */

pej xeem tsis muaj dab tsi horizontalMove (int kab, int col, Txoj hlua qhia) {…}

/* Sib piv ob lub pobzeb qhov tseem ceeb ua ke thiab yog lawv zoo ib yam lossis yog ib qho sib npaug rau 0 (pobzeb dawb) - lawv qhov txiaj ntsig tau ntxiv (muab tias cov vuas peb sib piv yog ob lub vuas sib txawv thiab lawv tau txav mus rau qhov kev coj ua uas tsim nyog) - rov ua dua los ntawm kab */

public void verticalMove (int kab, int col, Txoj hlua txoj kev) {…}

Yog, qhov ntawd muaj ntau txoj hauv kev - tab sis tsis txhob txhawj xeeb, feem ntau yog qhov yooj yim to taub. Ntxiv rau qhov ntawd, 'Pawg Neeg Saib Xyuas' chav kawm yog qhov nyuaj tshaj plaws, yog li txhua yam tom qab qhov no yuav yooj yim dua.

Kauj Ruam 3: Paub Paub Qhov Kev Pabcuam - Pobzeb

Pobzeb:

Nqe lus piav qhia: Chav Kawm Pobzeb cuam tshuam nrog cov vuas ib leeg, thiab yog qhov tsawg tshaj plaws ntawm txhua chav kawm. Txhua pobzeb muaj tus lej lej thiab xim. Nws muaj ob tus neeg tsim khoom uas tsim Cov Vuas ntawm tus nqi 0 (default) lossis tus nqi #. Cov txheej txheem feem ntau piav qhia tus kheej, nrog 'getter' thiab 'setter' cov txheej txheem ua rau feem ntau ntawm tag nrho.

Cov neeg tsim khoom:

/ * Tsim cov pobzeb yooj yim nrog tus nqi 0 */

pej xeem Pobzeb () {…}

/ * Tsim cov pobzeb nrog tus nqi ntawm tus lej */

pej xeem Pobzeb (int naj npawb) {…}

Txoj kev:

/ * Tau txais cov nplais tus nqi */

pej xeem int getValue () {…}

/ * Teem cov pobzeb tus nqi - siv thaum ntxiv ob lub vuas ua ke */

pej xeem tsis muaj dab tsi setValue (int tus nqi) {…}

/ * Sawv cev cov nplais raws li txoj hlua - siv hauv GUI */

public String toString () {…}

/ * Teeb cov nplais xim raws li nws tus nqi */

pej xeem void setColor () {…}

/ * Tau txais cov nplais xim */

pej xeem tsis muaj dab tsi getColor () {…}

Kauj Ruam 4: Paub Txog Txoj Haujlwm - Game

Game

Nqe lus piav qhia: Chav Kawm Hauv Tsev yog txoj hauv kev tseem ceeb, feem ntau ntawm GUI txoj hauv kev thiab Kev cuam tshuam tseem ceeb. Nws siv ob chav Pobzeb thiab Pawg Thawj Coj, thiab ua rau lawv ua haujlwm ua ke.

Cov neeg tsim khoom:

Tsis muaj

Txoj kev:

/ * teeb tsa GUI nrog qhov loj me uas tsim nyog thiab ntxiv Tus Neeg Ntsuam Xyuas Tseem Ceeb */

pej xeem tsis muaj teeb meem zoo li qub setUpGUI () {…}

/ * Txheeb xyuas seb puas yog wasd lossis xub tuav raug nias thiab ua qhov tsim nyog ua - hloov kho JFrame nrog txhua qhov txav */

pej xeem tsis muaj nuj nqis keyPressed (KeyEvent e) {…}

/ * Pleev xim GUI nrog cov hlua, pawg thawj coj saib, cov vuas thiab ua kom lawv rov ua dua thaum qhov kev ua si dhau lawm */

pej xeem tsis muaj xim (Graphics g) {…}

/ * kos tus kheej cov nplais - hu los ntawm cov xim txheej txheem */

pej xeem tsis muaj dab tsi drawTiles (Duab g, Pobzeb pobzeb, int x, int y) {…}

/ * Txoj hauv kev tseem ceeb - teeb tsa GUI thiab pib ua si */

pej xeem zoo li qub tsis muaj dab tsi tseem ceeb (Txoj hlua args) {…}

Kauj Ruam 5: Txoj hauv kev tseem ceeb - Kev txav chaw

Txoj hauv kev txav mus los yog qhov tseem ceeb tshaj plaws kom nkag siab, tab sis cov xov xwm zoo yog thaum koj nkag siab qhov kev txav mus los, koj tuaj yeem siv qhov kev nkag siab ntawd mus rau kab rov tav. Qhov tseeb, peb txoj kev txav mus los ntsug yog zoo ib yam li peb txoj kev txav mus los, tsuas yog ib qho txav hla kab thiab lwm yam hla kab. Vim li ntawd, cia peb tsom mus rau txoj hauv kev yooj yim txav mus los.

private void verticalMove (int kab, int col, Txoj hlua txoj kev)

{Pobzeb pib = board [ciam teb] [col]; Pobzeb sib piv = board [row] [col]; if (initial.getValue () == 0 || initial.getValue () == compare.getValue ()) {if (row> border || (direction.equals ("down") && (kab ntawv <ciam teb))) {int addScore = initial.getValue () + sib piv.getValue (); yog (initial.getValue ()! = 0) {qhab nias += addScore; } initial.setValue (addScore); sib piv.setValue (0); }} lwm {yog (direction.equals ("down")) {border--; } lwm {ciam teb ++; } verticalMove (kab, col, kev taw qhia); }}

Cov txheej txheem saum toj no, verticalMove, yog hu los ntawm txoj kev 'nce' thiab 'nqis'. Cia peb saib qhov txheej txheem 'nce'.

pej xeem tsis muaj dab tsi li ()

{rau (int i = 0; i <grids; i ++) {ciam teb = 0; rau (int j = 0; j <grids; j ++) {if (board [j] .getValue ()! = 0) {if (border <= j) {verticalMove (j, i, "up"); }

Txoj kev no hla mus rau tag nrho lub rooj tsavxwm thiab hu ntsug txav mus rau txhua lub pobzeb nrog qhov ntsuas "nce". verticalMove tom qab ntawd sib piv cov nplais ntawm txoj haujlwm 'j' thiab 'kuv' nrog cov nplais ntawm txoj haujlwm 'ciam teb' thiab 'kuv'. Yog tias ob leeg sib npaug, lawv tau sib koom ua ke. Yog tias lawv tsis yog, cov nplais ciam teb tau nce ntxiv 1 (raws li qhov ntsuas nyob hauv qhov chaw yog 'nce'), thiab ntsug txav tau hu dua.

Pom zoo: