insyspower
Newbie level 6
Cliente.java (Client)
Player.java
Servidor.java (Server)
Just copy paste some instances of the code (Tic-Tac-Toe Game).
My problem is this game as 2 players, each player plays 1 turn each time, then after being the other player turn. But my Game is Memory Game (Matching), each player plays 2 turns, if right, continues to play, is not right passes turn to another player, and vice versa until one player wins.
What i have to change in this code to each player plays 2 turns?, the turns are defined with mouse pressed.
public void setTurn()
This is my code of Memory Game.
Code Java - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public class Cliente extends JFrame implements ActionListener,Runnable { private Player players[] = null; private int numberOfPlayers; private int currentPlayer; boolean done = false; boolean myTurn = false; char myMark; public void createJPanels() { gamePnl.setLayout(new GridLayout(4, 4)); TrataRato tr = new TrataRato(); for (int i = 0; i < gameBtn.length; i++) { gamePnl.add(gameBtn[i]); gameBtn[i].addMouseListener(tr); public void processaMsgVez(MsgVez msg) throws Exception { if (msg.getEstado().equals("OK")) myTurn = true; else myTurn = false; } class TrataRato extends MouseAdapter { public void mouseReleased(MouseEvent evt) { if (!myTurn) { display2.setText("Wait for your turn.\n"); return; }
Player.java
Code Java - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 public class Player extends Thread { public Player(Socket s, Servidor t, int num ) { connection = s; control = t; number = num; mark = ( num == 0 ? 'X' : 'O' ); public void processaMsgPos(MsgPos msg) throws Exception { control.display( msg.toString() ); int location = msg.getPos(); if( control.validMove( location, number ) ) { MsgPos ans = new MsgPos(mark, location, "OK"); ans.envia(output); System.out.println("processaMsgPos: "+ans); if (control.getWinner() != ' ') control.setRes(); } else { MsgPos ans = new MsgPos(mark, location, "NOK"); System.out.println("processaMsgPos: "+ans.toString()); msg.envia(output); System.out.println("processaMsgPos: "+ans); } }
Servidor.java (Server)
Code Java - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 private int numberOfPlayers; private int currentPlayer; public class Servidor extends JFrame { public Servidor () { super( "Tic-Tac-Toe Server" ); Container cont = getContentPane(); board = new byte[9]; players = new Player[2]; currentPlayer = 0; public void setTurn() { try { players[currentPlayer].setTurn("OK"); players[(currentPlayer == 0 ? 1 : 0 )].setTurn("NOK"); } catch (Exception e) { e.printStackTrace(); } } public boolean validMove( int loc, int player ) { if( player != currentPlayer ) return false; if ( !isOccupied( loc ) ) { board[loc] = (byte)(currentPlayer == 0 ? 'X': 'O' ); currentPlayer = ++currentPlayer % 2; players[ currentPlayer ].otherPlayerMoved( loc );
Just copy paste some instances of the code (Tic-Tac-Toe Game).
My problem is this game as 2 players, each player plays 1 turn each time, then after being the other player turn. But my Game is Memory Game (Matching), each player plays 2 turns, if right, continues to play, is not right passes turn to another player, and vice versa until one player wins.
What i have to change in this code to each player plays 2 turns?, the turns are defined with mouse pressed.
public void setTurn()
This is my code of Memory Game.
Code Java - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 public void actionPerformed(ActionEvent e) { if (myTurn) { for (int i = 0; i < gameBtn.length; i++) { if (gameBtn[i] == e.getSource()) { gameBtn[i].setText("" + gameList.get(i)); gameBtn[i].setEnabled(false); counter++; if (counter == 3) { if (sameValues()) { gameBtn[btnID[0]].setEnabled(false); gameBtn[btnID[1]].setEnabled(false); gameBtn[btnID[0]].setVisible(true); gameBtn[btnID[1]].setVisible(true); Hit = Hit +1; Pontos = Pontos + 25; try { players[currentPlayer].setTurn("OK"); //players[(currentPlayer == 0 ? 1 : 0 )].setTurn("NOK"); } catch (Exception y) { y.printStackTrace(); } //currentPlayer = 0; myTurn = true; } else { gameBtn[btnID[0]].setEnabled(true); gameBtn[btnID[0]].setText(""); gameBtn[btnID[1]].setEnabled(true); gameBtn[btnID[1]].setText(""); Miss = Miss +1; Pontos = Pontos - 5; try { //players[currentPlayer].setTurn("OK"); players[(currentPlayer == 0 ? 1 : 0 )].setTurn("NOK"); } catch (Exception y) { y.printStackTrace(); } myTurn = false; currentPlayer = 1; } counter = 1; } if (counter == 1) // on grid identifies 1º click of mouse { btnID[0] = i; btnValue[0] = gameList.get(i); } if (counter == 2) // on grid identifies 2º click of mouse { btnID[1] = i; btnValue[1] = gameList.get(i); } } }
Last edited by a moderator: