Skip to content
Snippets Groups Projects
Commit 81b3e365 authored by Tarek's avatar Tarek Committed by Ricardo Müller
Browse files

Removed unnecessary class GameBoard

parent a3b728f2
No related branches found
No related tags found
1 merge request!9Cleaned up and re-added GameBoard
package com.example.tictactoeswtha2;
public class GameBoard {
/**
* 3x3 game board
*/
private int[][] board = new int[3][3];
/**
* Initialize every position with 0
*/
public void GameBoard(){
for (int y = 0; y < 3; y++){
for (int x = 0; x < 3; x++){
board[y][x] = 0;
}
}
}
/**
* Tries to place a symbol on the board
* @param position Position where the symbol should be placed 1-9
* @param symbol Symbol that should be placed
* @return Whether the symbol could be placed
*/
public boolean addSymbol(int position, int symbol){
int x = position % 3 + 1;
int y = position / 3;
if (board[y][x] > 0)
return false;
board[y][x] = symbol;
return true;
}
/**
* Checks if the game is finished
* 0: game isn't finished yet
* 1: Player 1 won
* 2: Player 2 won
* 3: tie
* @return Winner or tie or unfinished
*/
public int winCheck(){
for (int i = 0; i < 3; i++){
// vertical
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] > 0)
return board[0][i];
// horizontal
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] > 0)
return board[i][0];
}
// diagonal
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] > 0)
return board[1][1];
if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] > 0)
return board[1][1];
// game isn't finished
for (int i = 0; i < 9; i++) {
int x = i % 3 + 1;
int y = i / 3;
if (board[y][x] == 0)
return 0;
}
// Tie
return 3;
}
/**
* Gives back the game board to display it
* @return Game board
*/
public int[][] getBoard() {
return board;
}
/**
* Prints the board on the console for debugging
*/
public void printOnConsole(){
for (int y = 0; y < 3; y++){
for (int x = 0; x < 3; x++){
System.out.printf(Integer.toString(board[y][x]));
}
System.out.printf("\n");
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment