Jenkins
3 years ago
3 changed files with 192 additions and 6 deletions
-
74src/main/java/TicTacToe/TicTacToeGame.java
-
49src/main/java/TicTacToe/cell.java
-
69src/test/java/TicTacToe/TicTacToeGameTest.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)); |
|||
} |
|||
|
|||
} |
@ -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 { |
|||
|
|||
@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<Arguments> 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<Arguments> 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}) |
|||
); |
|||
|
|||
|
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue