diff --git a/src/main/java/TicTacToe/TicTacToeGame.java b/src/main/java/TicTacToe/TicTacToeGame.java index f958273..acef34d 100644 --- a/src/main/java/TicTacToe/TicTacToeGame.java +++ b/src/main/java/TicTacToe/TicTacToeGame.java @@ -5,29 +5,56 @@ import java.awt.Graphics2D; import java.awt.geom.Line2D; import javax.swing.JFrame; +import javax.swing.JOptionPane; 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 playerID = 1; + public int turns = 0; + public TicTacToeGame() { this.setSize(width, height); + setLayout(null); + initField(); } public static void main(String[] args) { - JFrame f = new JFrame(); TicTacToeGame ttt = new TicTacToeGame(); - + f.add(ttt); f.setSize(width,height); f.setLayout(null); f.setVisible(true); } + public void initField() { + field = new cell[playFieldSize]; + + for(int i = 0; i < field.length; i++) { + field[i] = new cell(this); + add(field[i]); + } + + for(int i = 0; i < field.length; i++) { + if(i < 3) { + field[i].setBounds(150 + i*100, 150 , 100, 100); + } else if (i < 6) { + field[i].setBounds(150 + i%3*100, 250 , 100, 100); + } else { + field[i].setBounds(150 + i%3*100, 350 , 100, 100); + } + } + } + @Override protected void paintComponent(Graphics g) { super.paintComponent(g); @@ -44,5 +71,48 @@ public class TicTacToeGame extends JPanel { g2.draw(lin4); } + public void endTurn() { + if(checkPlayfield() != 0) { + JOptionPane.showMessageDialog(getParent(),"Player: " + playerID + " Wins!"); + resetGame(); + } + + turns++; + if(turns >= playFieldSize) { + JOptionPane.showMessageDialog(getParent(),"Draw!"); + resetGame(); + } + + playerID++; + if(playerID >= maxPlayers) { + playerID = 1; + } + } + + public int checkPlayfield() { + 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; + } + + public void resetGame() { + for (cell c : field) { + c.reset(); + } + playerID = 1; + turns = 0; + } + } \ No newline at end of file diff --git a/src/main/java/TicTacToe/cell.java b/src/main/java/TicTacToe/cell.java new file mode 100644 index 0000000..27a3158 --- /dev/null +++ b/src/main/java/TicTacToe/cell.java @@ -0,0 +1,49 @@ +package TicTacToe; + +import java.awt.Color; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; + +public class cell extends JButton { + + public int playerID = 0; + private TicTacToeGame ttt; + + public cell(TicTacToeGame _ttt) { + ttt = _ttt; + setBackground(new Color(255,255,255)); + + addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + OnMouseClick(); + } + }); + } + + protected void OnMouseClick() { + if (playerID == 0) { + playerID = ttt.playerID; + ttt.endTurn(); + setEnabled(false); + + switch (playerID) { + case 1: + setBackground(new Color(255, 0, 0)); + break; + case 2: + setBackground(new Color(0, 0, 255)); + break; + } + } + } + + protected void reset() { + playerID = 0; + setEnabled(true); + setBackground(new Color(255,255,255)); + } + +} diff --git a/src/test/java/TicTacToe/TicTacToeGameTest.java b/src/test/java/TicTacToe/TicTacToeGameTest.java index 08742dd..beaabde 100644 --- a/src/test/java/TicTacToe/TicTacToeGameTest.java +++ b/src/test/java/TicTacToe/TicTacToeGameTest.java @@ -1,10 +1,77 @@ package TicTacToe; import static org.junit.jupiter.api.Assertions.*; -import org.junit.jupiter.api.Test; +import java.util.stream.Stream; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; -class TicTacToeGameTest { +class TicTacToeGameTest { + + @ParameterizedTest + @MethodSource("testFieldsWinning") + void testGameEndWin(int[] _field, int _winner) { + TicTacToeGame ttt = new TicTacToeGame(); + + for(int i = 0; i < ttt.field.length; i++) { + ttt.field[i].playerID = _field[i]; + } + ttt.playerID = _winner; + int realWinner = ttt.checkPlayfield(); + + assertEquals(_winner, realWinner); + } + + @ParameterizedTest + @MethodSource("testFieldsDraw") + void testGameEndDraw(int[] _field) { + TicTacToeGame ttt = new TicTacToeGame(); + + for(int i = 0; i < ttt.field.length; i++) { + ttt.field[i].playerID = _field[i]; + } + + int noWinner = ttt.checkPlayfield(); + + assertEquals(0, noWinner); + } + private static Stream testFieldsWinning(){ + return Stream.of( + Arguments.of(new int[]{ 1,2,1, + 2,2,2, + 1,2,1}, 2), + Arguments.of(new int[]{ 2,1,2, + 2,2,1, + 1,1,1}, 1), + Arguments.of(new int[]{ 1,1,2, + 1,2,2, + 1,2,1}, 1), + Arguments.of(new int[]{ 2,1,1, + 1,2,1, + 1,1,2}, 2) + ); + } + + private static Stream testFieldsDraw(){ + return Stream.of( + Arguments.of(new int[]{ 2,1,1, + 2,2,2, + 1,2,1}), + Arguments.of(new int[]{ 2,1,2, + 2,2,1, + 1,2,1}), + Arguments.of(new int[]{ 2,1,2, + 1,2,2, + 1,2,1}), + Arguments.of(new int[]{ 2,1,1, + 1,2,2, + 2,1,1}) + ); + + + } }