I'm writing a Boolean Tic Tac Toe method that checks for a winner through diagonals and crosses. I've got it working, but it seemed really inefficient and unnecessarily redundant; I can't think up of a way to simplify it. Can you guys see a way? Thanks!
public boolean winner(int player)
{
int count = 0;
for(int r=0;r<3;r++){ // top left to bottom right
if(board[r][r].getPlayer()==player){
count++;
}
}
if(count==3){
return true;
}else{
count = 0;
int c = 2;
for(int r=0;r<3;r++){ // top right to bottom left
if(board[r][c--].getPlayer()==player){
count++;
}
}
}
if(count==3){
return true;
}else{
count = 0;
for(int r=0;r<3;r++){ // top to bottom from col 0
if(board[r][0].getPlayer()==player){
count++;
}
}
}
if(count==3){
return true;
}else{
count = 0;
for(int r=0;r<3;r++){ // top to bottom from col 1
if(board[r][1].getPlayer()==player){
count++;
}
}
}
if(count==3){
return true;
}else{
count = 0;
for(int r=0;r<3;r++){ // top to bottom from col 2
if(board[r][2].getPlayer()==player){
count++;
}
}
}
if(count==3){
return true;
}else{
count = 0;
for(int c=0;c<3;c++){ // left to right from row 0
if(board[0][c].getPlayer()==player){
count++;
}
}
}
if(count==3){
return true;
}else{
count = 0;
for(int c=0;c<3;c++){ // left to right from row 1
if(board[1][c].getPlayer()==player){
count++;
}
}
}
if(count==3){
return true;
}else{
count = 0;
for(int c=0;c<3;c++){ // left to right from row 2
if(board[2][c].getPlayer()==player){
count++;
}
}
}
if(count==3){
return true;
}
return false;
}
