Skip to content

Commit 23f42e1

Browse files
authored
Merge pull request Diffblue-benchmarks#7 from DiffBlue-benchmarks/tic-tac-toe-string
Check tic tac toe with strings
2 parents 4e3e1f9 + 3fb0bbe commit 23f42e1

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

main-module/src/main/java/com/diffblue/java_test/Additions.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,41 @@ else if (a[2] == 2)
7373
return 0;
7474
}
7575

76+
public String checkTicTacToePosition(char[] a, char[] b, char[] c) {
77+
if (a.length != 3 || b.length != 3 || c.length != 3) {
78+
return "Error: Lines not equal length";
79+
}
80+
byte[] bytes = new byte[9];
81+
for (int i=0; i<9; i++) {
82+
bytes[i] = ticTacToeCharToByte(i<3 ? a[i]: i<6 ? b[i-3] : c[i-6]);
83+
}
84+
switch (checkTicTacToePosition(bytes)) {
85+
case 1:
86+
return "X wins";
87+
case 2:
88+
return "O wins";
89+
default:
90+
return "No winner";
91+
}
92+
}
93+
94+
static byte ticTacToeCharToByte(char c) {
95+
switch (c) {
96+
case 'X':
97+
case 'x':
98+
return 1;
99+
case 'O':
100+
case 'o':
101+
case '0':
102+
return 2;
103+
case ' ':
104+
return 0;
105+
default:
106+
throw new Error("Character " + c + " not regognized");
107+
}
108+
}
109+
110+
76111
/*
77112
* checks if an array contains a sequence
78113
* [... '<','h','t','m','l' ...]

0 commit comments

Comments
 (0)