Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Java Algorithm for Determining Players Turns (Tic-Tac-Toe to Memory Game)

Status
Not open for further replies.

insyspower

New Member
Cliente.java (Client)
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);
gameBtn.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

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)
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.

public void actionPerformed(ActionEvent e)
{
if (myTurn)
{
for (int i = 0; i < gameBtn.length; i++)
{
if (gameBtn == e.getSource())
{
gameBtn.setText("" + gameList.get(i));
gameBtn.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);
}
}
}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top