|
|
@ -12,9 +12,12 @@ import javax.swing.JPanel; |
|
|
|
public class TicTacToeGame extends JPanel { |
|
|
|
|
|
|
|
private static final long serialVersionUID = 1L; |
|
|
|
private static int width = 600, height = 600; |
|
|
|
private static final int width = 600, height = 600; |
|
|
|
private static final int maxPlayers = 3; |
|
|
|
private static final int playFieldSize = 9; |
|
|
|
|
|
|
|
public cell[] field; |
|
|
|
public int player = 1; |
|
|
|
public int playerID = 1; |
|
|
|
|
|
|
|
public TicTacToeGame() { |
|
|
|
this.setSize(width, height); |
|
|
@ -33,7 +36,7 @@ public class TicTacToeGame extends JPanel { |
|
|
|
} |
|
|
|
|
|
|
|
public void initField() { |
|
|
|
field = new cell[9]; |
|
|
|
field = new cell[playFieldSize]; |
|
|
|
|
|
|
|
for(int i = 0; i < field.length; i++) { |
|
|
|
field[i] = new cell(this); |
|
|
@ -68,30 +71,29 @@ public class TicTacToeGame extends JPanel { |
|
|
|
} |
|
|
|
|
|
|
|
public void endTurn() { |
|
|
|
|
|
|
|
if(checkPlayfield() != 0) { |
|
|
|
JOptionPane.showMessageDialog(getParent(),"Player: " + player + " Wins!"); |
|
|
|
JOptionPane.showMessageDialog(getParent(),"Player: " + playerID + " Wins!"); |
|
|
|
System.exit(0); |
|
|
|
} |
|
|
|
|
|
|
|
player++; |
|
|
|
if(player >= 3) { |
|
|
|
player = 1; |
|
|
|
playerID++; |
|
|
|
if(playerID >= maxPlayers) { |
|
|
|
playerID = 1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public int checkPlayfield() { |
|
|
|
if ((field[0].value == player && field[1].value == player && field[2].value == player) |
|
|
|
|| (field[0].value == player && field[3].value == player && field[6].value == player) |
|
|
|
|| (field[8].value == player && field[5].value == player && field[2].value == player) |
|
|
|
|| (field[8].value == player && field[7].value == player && field[6].value == player) |
|
|
|
|| (field[0].value == player && field[4].value == player && field[8].value == player) |
|
|
|
|| (field[0].value == player && field[4].value == player && field[8].value == player) |
|
|
|
|| (field[2].value == player && field[4].value == player && field[6].value == player) |
|
|
|
|| (field[3].value == player && field[4].value == player && field[5].value == player) |
|
|
|
|| (field[1].value == player && field[4].value == player && field[7].value == player)) { |
|
|
|
|
|
|
|
return player; |
|
|
|
if ((field[0].playerID == playerID && field[1].playerID == playerID && field[2].playerID == playerID) |
|
|
|
|| (field[0].playerID == playerID && field[3].playerID == playerID && field[6].playerID == playerID) |
|
|
|
|| (field[8].playerID == playerID && field[5].playerID == playerID && field[2].playerID == playerID) |
|
|
|
|| (field[8].playerID == playerID && field[7].playerID == playerID && field[6].playerID == playerID) |
|
|
|
|| (field[0].playerID == playerID && field[4].playerID == playerID && field[8].playerID == playerID) |
|
|
|
|| (field[0].playerID == playerID && field[4].playerID == playerID && field[8].playerID == playerID) |
|
|
|
|| (field[2].playerID == playerID && field[4].playerID == playerID && field[6].playerID == playerID) |
|
|
|
|| (field[3].playerID == playerID && field[4].playerID == playerID && field[5].playerID == playerID) |
|
|
|
|| (field[1].playerID == playerID && field[4].playerID == playerID && field[7].playerID == playerID)) { |
|
|
|
|
|
|
|
return playerID; |
|
|
|
} |
|
|
|
|
|
|
|
return 0; |
|
|
|