0% found this document useful (0 votes)
36 views8 pages

Untitled

Download as txt, pdf, or txt
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 8

import javax.microedition.lcdui.

*; // Referenced classes of package JCaro: // Table, JCaro public class GameCanvas extends Canvas implements CommandListener { int userid; int comid; int NOTHING; int PREPARE; int KEYMOVE; int DISPLAYRESULT; int KEYENTER; int DISPLAYHELP; int REFRESH; boolean userwin; int status; int bannerwidth; int bannerheight; int bannerx; int bannery; int size; boolean humanfirst; Command cmdSize; Command cmdLevel; Command cmdFirst; Command cmdHelp; Command cmdReturn; int m; int n; int d; int ox; int oy; int x; int y; int xc; int yc; int xold; int yold; Table table; int drawX; int drawY; int drawStatus; int backcolor; int bordercolor; int selectcolor; public GameCanvas() { userid = 1; comid = 2; NOTHING = 0; PREPARE = 1; KEYMOVE = 2; DISPLAYRESULT = 3; KEYENTER = 4; DISPLAYHELP = 5;

REFRESH = 6; userwin = true; status = 0; bannerwidth = 80; bannerheight = 40; bannerx = (getWidth() - bannerwidth) / 2; bannery = getHeight() - bannerheight - 15; size = 1; humanfirst = true; d = getWidth() / 14; table = new Table(); backcolor = 0xffffff; bordercolor = 0x969696; selectcolor = 0xd1e25a; try { setCommandListener(this); addCommand(new Command("Exit", 7, 1)); addMenu(); } catch(Exception exception) { exception.printStackTrace(); } update(PREPARE); } private void addMenu() { if(size == 0) { cmdSize = new Command("Size : } else if(size == 1) { cmdSize = new Command("Size : } else if(size == 2) { cmdSize = new Command("Size : } addCommand(cmdSize); if(table.level == 0) { cmdLevel = new Command("Level } else if(table.level == 1) { cmdLevel = new Command("Level } else if(table.level == 2) { cmdLevel = new Command("Level } addCommand(cmdLevel); if(humanfirst) { cmdFirst = new Command("First } else {

Small", 4, 1);

Standard", 4, 1);

Large", 4, 1);

: Easy", 4, 2);

: Normal", 4, 2);

: Hard", 4, 2);

: You", 4, 3);

cmdFirst = new Command("First : Computer", 4, 3); } addCommand(cmdFirst); cmdHelp = new Command("Help", 4, 4); cmdReturn = new Command("Return", 4, 5); addCommand(cmdHelp); addCommand(cmdReturn); } public void commandAction(Command command, Displayable displayable) { if(command.getCommandType() ==7) { JCaro.quitApp(); } else if(command.getCommandType() == 4) { if(command.getPriority() ==1) { size = (size + 1) % 3; if(size == 0) { d = getWidth() / 10; } else if(size == 1) { d = getWidth() / 14; } else if(size == 2) { d = getWidth() / 18; } } else if(command.getPriority() == 2) { table.level = (table.level + 1) % 3; } else if(command.getPriority() == 3) { humanfirst = !humanfirst; } else if(command.getPriority() == 4) { update(DISPLAYHELP); return; } else { update(REFRESH); return; } update(PREPARE); removeCommand(cmdSize); removeCommand(cmdLevel); removeCommand(cmdFirst); removeCommand(cmdHelp); removeCommand(cmdReturn); addMenu(); } }

2, 2, 2, 2,

public void drawCell(Graphics g, int i, int j, int k) { if(k == 0) { g.setColor(backcolor); } else { g.setColor(selectcolor); } g.fillRect(ox + i * d, oy + j * d, d, d); g.setColor(bordercolor); g.drawRect(ox + i * d, oy + j * d, d, d); if(i >= 0 && j >= 0 && i < m && j < n) { if(table.cell[i][j] == 1) { g.setColor(255, 0, 0); g.drawLine(ox + i * d + 2, oy + j * d + 2, (ox + (i + 1) * d) (oy + (j + 1) * d) - 2); g.drawLine(ox + i * d + 2, oy + j * d + 3, (ox + (i + 1) * d) (oy + (j + 1) * d) - 3); g.drawLine((ox + (i + 1) * d) - 2, oy + j * d + 2, ox + i * d + (oy + (j + 1) * d) - 2); g.drawLine((ox + (i + 1) * d) - 2, oy + j * d + 3, ox + i * d + (oy + (j + 1) * d) - 3); } else if(table.cell[i][j] == 2) { g.setColor(0, 0, 255); g.drawArc(ox + i * d + 1, oy + j * d + 1, d - 2, d - 2, 0, 360); g.drawArc(ox + i * d + 2, oy + j * d + 2, d - 4, d - 4, 0, 360); } } } protected void paint(Graphics g) { if(status == PREPARE) { m = getWidth() / d; n = getHeight() / d; ox = (getWidth() - m * d) / 2; oy = (getHeight() - n * d) / 2; table.cell = new int[m][n]; table.score = new int[m][n]; table.score1 = new int[m][n]; table.m = m; table.n = n; for(int i = 0; i < m; i++) { for(int k1 = 0; k1 < n; k1++) { table.cell[i][k1] = 0; } } g.setColor(backcolor); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(bordercolor);

for(int j = 0; j <= m; j++) { g.drawLine(ox + j * d, 0, ox + j * d, getHeight()); } for(int k = 0; k <= n; k++) { g.drawLine(0, oy + k * d, getWidth(), oy + k * d); } x = xold = m / 2; y = yold = n / 2; if(!humanfirst) { table.cell[x][y] = comid; } drawCell(g, x, y, 1); } else if(status == KEYMOVE) { drawCell(g, xold, yold, 0); drawCell(g, x, y, 1); xold = x; yold = y; } else if(status == KEYENTER) { drawCell(g, xc, yc, 0); drawCell(g, x, y, 1); xold = x; yold = y; } else if(status == REFRESH) { g.setColor(backcolor); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(bordercolor); for(int l = 0; l <= m; l++) { g.drawLine(ox + l * d, 0, ox + l * d, getHeight()); } for(int i1 = 0; i1 <= n; i1++) { g.drawLine(0, oy + i1 * d, getWidth(), oy + i1 * d); } for(int j1 = 0; j1 < m; j1++) { for(int l1 = 0; l1 < n; l1++) { if(table.cell[j1][l1] != 0) { drawCell(g, j1, l1, 0); } } } drawCell(g, x, y, 1);

} else if(status != DISPLAYHELP && status == DISPLAYRESULT) { drawCell(g, x, y, 0); drawCell(g, xc, yc, 0); if(userwin) { g.setColor(0, 255, 0); g.fillRect(bannerx, bannery, bannerwidth, bannerheight); g.setColor(bordercolor); g.drawRect(bannerx, bannery, bannerwidth, bannerheight); g.drawRect(bannerx + 1, bannery + 1, bannerwidth - 2, bannerheig ht - 2); g.setColor(0); g.setFont(Font.getFont(0, 1, 0)); g.drawString("You win", bannerx + 12, bannery + 2, 0); g.drawString("Press 1...", bannerx + 10, bannery + 18, 0); } else { g.setColor(255, 0, 0); g.fillRect(bannerx, bannery, bannerwidth, bannerheight); g.setColor(bordercolor); g.drawRect(bannerx, bannery, bannerwidth, bannerheight); g.drawRect(bannerx + 1, bannery + 1, bannerwidth - 2, bannerheig ht - 2); g.setColor(backcolor); g.setFont(Font.getFont(0, 1, 0)); g.drawString("You lose", bannerx + 10, bannery + 2, 0); g.drawString("Press 1...", bannerx + 10, bannery + 18, 0); } } } void update(int i) { status = i; repaint(); } public void keyPressed(int i) { if(status == DISPLAYRESULT) { if(i == 49) { update(PREPARE); } return; } if(status == DISPLAYHELP) { if(i == 49) { update(REFRESH); } return; } int j = getGameAction(i); switch(j) {

case 3: // case 4: // case 7: // case 8: // default: break;

'\003' '\004' '\007' '\b'

case 2: // '\002' if(x > 0) { x--; } else { x = m - 1; } update(KEYMOVE); break; case 5: // '\005' if(x < m - 1) { x++; } else { x = 0; } update(KEYMOVE); break; case 1: // '\001' if(y > 0) { y--; } else { y = n - 1; } update(KEYMOVE); break; case 6: // '\006' if(y < n - 1) { y++; } else { y = 0; } update(KEYMOVE); break; case 9: // '\t' if(table.cell[x][y] != 0) { return; } table.cell[x][y] = userid; if(table.checkwin()) { userwin = true;

update(DISPLAYRESULT); return; } table.findSolution(); table.cell[table.resX][table.resY] = comid; xc = table.resX; yc = table.resY; if(table.checkwin()) { userwin = false; update(DISPLAYRESULT); return; } update(KEYENTER); break; } } }

You might also like