11. marts 2004 - 17:50
Der er
12 kommentarer og 1 løsning
Problem med kryds og bolle spil
Hey Jeg sidder og roder med et lille kryds og bolle spil. Men jeg har et problem som er ved at give mig grå hår :S Når man klikker på et felt for henholdsvis at placere et kryds eller en bolle, så ska man klikke to gange før den første "brik" bliver sat. Efterfølgende virker det fint. Her er min paint metode: if(gameState == GAME_STATE_INTRO) { g.drawString("Kryds og bolle :)", 100, 100); } else if(gameState == GAME_STATE_START) { // tegn bræt m.m. board.draw(g,Color.red, tur); writeMessage(g); board.drawPieces(g); } Her er den metode som skal tegne brikkerne: public void drawPieces(Graphics g) { int _x = this.width / 3; int _y = this.height / 3; int i = 0; Piece piece = new Piece(); while(i < pieces.size()) { piece = (Piece) pieces.elementAt(i); g.drawImage(piece.pic, this.offset + _x*piece.x + 2, this.offset + _y*piece.y +2, null); i++; } } og koden til at placere en brik: int i = -1; if(board.convertPos(mouse).x != -1 && board.convertPos(mouse).y != -1) { i = board.setPiece(tur, board.convertPos(mouse).x, board.convertPos(mouse).y); if(i == 0) tur(); else if(i == 2) setMessage(1, "Feltet er allerede fyldt!", Color.red); } else { setMessage(1, "Ugyldigt felt!", Color.red); } og setPiece() : public int setPiece(int tur, int cellx, int celly) { Piece piece; if(cells[cellx][celly] == NOT_ASSIGNED) { cells[cellx][celly] = tur; if(tur == 0) piece = new Piece(circle); else piece = new Piece(square); piece.assign(cellx, celly); pieces.add(piece); return 0; } else return CELL_ALREADY_USED; } håber i ka finde problemet :)
Annonceindlæg fra Kingston Technology
13. marts 2004 - 16:54
#9
Her kommer den så: ------------------------------------------------------------------------------ Applet_tictac.java: import java.awt.*; import java.applet.*; import java.awt.event.*; import java.net.*; public class Applet_tictac extends Applet implements MouseListener, MouseMotionListener { public final static int APP_WIDTH = 210; public final static int APP_HEIGHT = 210; public final static int APP_MARGIN = 10; public final static int GAME_STATE_INTRO = 0; public final static int GAME_STATE_START = 1; public Graphics getG() { return this.getGraphics(); } Board board; int gameState; Point mouse; Player pl1, pl2; int tur = -1; URL base; Image circle; Image square; private String[] message; private Color[] msg_color; public void init() { board = new Board(APP_WIDTH, APP_HEIGHT, APP_MARGIN); gameState = GAME_STATE_INTRO; addMouseListener(this); addMouseMotionListener(this); mouse = new Point(); message = new String[3]; msg_color = new Color[3]; for(int i = 0; i<3; i++) { message[i] = new String(); msg_color[i] = Color.black; } pl1 = new Player("Spiller1", Color.blue); pl2 = new Player("Spiller2", Color.blue); board.setPlayers(pl1, pl2); try { base = getCodeBase(); } catch (Exception e) {} circle = getImage(base, "circle.gif"); square = getImage(base, "cross.gif"); board.setImages(circle, square); } public void paint(Graphics g) { if(gameState == GAME_STATE_INTRO) { g.drawString("Kryds og bolle :)", 100, 100); } else if(gameState == GAME_STATE_START) { // tegn bræt board.draw(g,Color.red, tur); writeMessage(g); board.drawPieces(g); } paintComponents(g); } public void mouseReleased(MouseEvent evt) { if(gameState == GAME_STATE_INTRO) { gameState = GAME_STATE_START; tur(); repaint(); } else if(gameState == GAME_STATE_START) { int i = -1; if(board.convertPos(mouse).x != -1 && board.convertPos(mouse).y != -1) { i = board.setPiece(tur, board.convertPos(mouse).x, board.convertPos(mouse).y); if(i == 0) tur(); else if(i == 2) setMessage(1, "Feltet er allerede fyldt!", Color.red); } else { setMessage(1, "Ugyldigt felt!", Color.red); } if(i != 0) setMessage(0, "Det er nu " + board.players[tur].getName() + "'s tur", board.players[tur].getColor()); repaint(); } } public void tur() { if(this.tur == -1) { tur = 0; } else { if(tur == 0) tur = 1; else tur = 0; } setMessage(0, "Det er nu " + board.players[tur].getName() + "'s tur", board.players[tur].getColor()); setMessage(1, "", Color.black); } public void mousePressed(MouseEvent evt) {} public void mouseExited(MouseEvent evt) {} public void mouseEntered(MouseEvent evt) {} public void mouseClicked(MouseEvent evt) {} public void mouseDragged(MouseEvent evt) {} public void mouseMoved(MouseEvent evt) { mouse.x = evt.getX(); mouse.y = evt.getY(); evt.consume(); } public void setMessage(int index, String msg, Color color) { this.message[index] = msg; this.msg_color[index] = color; } public void writeMessage(Graphics g) { // tegn besked-boks g.setColor(Color.black); g.drawLine(APP_MARGIN, 2*APP_MARGIN + APP_HEIGHT, APP_MARGIN + 210, 2*APP_MARGIN + APP_HEIGHT); g.drawLine(APP_MARGIN, 2*APP_MARGIN + APP_HEIGHT + 60, APP_MARGIN + 210, 2*APP_MARGIN + APP_HEIGHT + 60); g.drawLine(APP_MARGIN, 2*APP_MARGIN + APP_HEIGHT, APP_MARGIN, 2*APP_MARGIN + APP_HEIGHT+60); g.drawLine(APP_MARGIN+210, 2*APP_MARGIN + APP_HEIGHT, APP_MARGIN + 210, 2*APP_MARGIN + APP_HEIGHT+60); // skriv en eventuel besked for(int i = 0; i<3; i++) { if(this.message[i].length() > 0) { g.setColor(this.msg_color[i]); g.drawString(this.message[i], 2*APP_MARGIN, 4*APP_MARGIN + APP_HEIGHT + i*(APP_MARGIN+5)); } } } } ----------------------------------------------------- Board.java import java.awt.*; import java.util.Vector; public class Board { public static final int NOT_ASSIGNED = -1; public static final int CELL_ALREADY_USED = 2; public static final int CELl_INVALID = 3; private int width; private int height; private int offset; private int[][] cells; private Image circle; private Image square; private Vector pieces; private int piecesSet = 0; private int[][][] pi; public Player[] players; public Board(int width, int height, int offset) { this.width = width; this.height = height; this.offset = offset; cells = new int[3][3]; for(int i = 0; i < 3;i++) { for(int j = 0; j < 3; j++) { cells[i][j] = -1; } } players = new Player[2]; pieces = new Vector(); } public int setPiece(int tur, int cellx, int celly) { Piece piece; if(cells[cellx][celly] == NOT_ASSIGNED) { cells[cellx][celly] = tur; if(tur == 0) piece = new Piece(circle); else piece = new Piece(square); piece.assign(cellx, celly); pieces.add(piece); return 0; } else return CELL_ALREADY_USED; } public void setPlayers(Player pl1, Player pl2) { players[0] = pl1; players[1] = pl2; } public void setImages(Image circle, Image square) { this.circle = circle; this.square = square; } public void draw(Graphics g, Color color, int tur) { // tegn ydre ramme g.setColor(color); g.drawLine(this.offset, this.offset, this.offset + this.width, this.offset); g.drawLine(this.offset, this.offset, this.offset, this.offset + this.height); g.drawLine(this.offset + this.width, this.offset, this.offset + this.width, this.offset + this.height); g.drawLine(this.offset, this.offset + this.height, this.offset + this.width, this.offset + this.height); // tegn tern int _x = this.width / 3; int _y = this.height / 3; g.drawLine(offset + _x, offset, offset + _x, offset + height); g.drawLine(offset + 2* _x, offset, offset + 2* _x, offset + height); g.drawLine(offset, offset + _y, offset + width, offset + _y); g.drawLine(offset, offset + 2*_y, offset + width, offset +2* _y); } public void drawPieces(Graphics g) { int _x = this.width / 3; int _y = this.height / 3; int i = 0; Piece piece = new Piece(); while(i < pieces.size()) { piece = (Piece) pieces.elementAt(i); g.drawImage(piece.pic, this.offset + _x*piece.x + 2, this.offset + _y*piece.y +2, null); i++; } } public Point convertPos(Point mouse) { Point cell = new Point(); // er punktet indenfor rammen? if(mouse.x < this.offset || mouse.x > this.offset + this.width) cell.x = -1; if(mouse.y < this.offset || mouse.y > this.offset + this.height) cell.y = -1; if(cell.x == -1 || cell.y == -1) { cell.x = cell.y = -1; return cell; } // bestem celledimension int _x = this.width / 3; int _y = this.height / 3; // hvilken celle er punktet i? if(mouse.x < this.offset + _x) cell.x = 0; // celle 1 else if(mouse.x < this.offset + 2*_x) cell.x = 1; // celle 2 else cell.x = 2; // celle 3 if(mouse.y < this.offset + _y) cell.y = 0; // celle 1 else if(mouse.y < this.offset + 2*_y) cell.y = 1; // celle 2 else cell.y = 2; // celle 3 return cell; } } ----------------------------------------------------------------------- Player.java import java.awt.*; import java.util.Vector; class Player { private String name; private Vector Pieces; private Color color; private int piecesSet = 0; public Player(String name, Color color) { this.name = new String(name); this.color = color; } public String getName() { return this.name; } public Color getColor() { return this.color; } public void addPiece() { this.piecesSet++; } public int piecesLeft() { return 4-this.piecesSet; } } ------------------------------------------------------------------- Piece.java import java.awt.*; class Piece { public static final int NOT_ASSIGNED = -1; public int x, y; public Image pic; public Piece(Image _pic) { pic = _pic; x = y = NOT_ASSIGNED; } public Piece() { } public void assign(int _x, int _y) { x = _x; y = _y; } } ------------- der var den :)