Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tictactoe;
- import java.util.Scanner;
- public class TicTacToe {
- private TicTacToePlayer[][] spelBord;
- private String puzzelString;
- public TicTacToe() {
- spelBord = new TicTacToePlayer[3][3];
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- this.spelBord[i][j]=null;
- }
- }
- }
- public TicTacToePlayer getCell(int row, int column){
- if (row>=1&&row<=3 &&column>=1&&column<=3){
- return spelBord[row-1][column-1];
- }else{
- return null;
- }
- }
- public TicTacToePlayer getCurrentTurn(){
- int aantalX = 0;
- int aantalO = 0;
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- if (this.spelBord[i][j]==TicTacToePlayer.X){
- aantalX++;
- } else if (this.spelBord[i][j]==TicTacToePlayer.O){
- aantalO++;
- }
- }
- }
- if (aantalX > aantalO) {
- return TicTacToePlayer.O;
- }
- return TicTacToePlayer.X;
- }
- public TicTacToePlayer getWinner(){
- int aantalX = 0;
- int aantalO = 0;
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- if (this.spelBord[i][j]==TicTacToePlayer.X){
- aantalX++;
- if (aantalX == 3) {return TicTacToePlayer.X;}
- }
- if (this.spelBord[i][j]==TicTacToePlayer.O){
- aantalO++;
- if (aantalO == 3) {return TicTacToePlayer.O;}
- }
- }
- aantalX = 0;
- aantalO = 0;
- }
- // en herbeginnen maar voor de rijen
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- if (this.spelBord[j][i]==TicTacToePlayer.X){
- aantalX++;
- if (aantalX == 3) {return TicTacToePlayer.X;}
- }
- if (this.spelBord[j][i]==TicTacToePlayer.O){
- aantalO++;
- if (aantalO == 3) {return TicTacToePlayer.O;}
- }
- }
- aantalX = 0;
- aantalO = 0;
- }
- // en de diagonaal linksboven naar rechtsonder
- for (int i = 0; i < 3; i++) {
- if (this.spelBord[i][i]==TicTacToePlayer.X){
- aantalX++;
- if (aantalX == 3) {return TicTacToePlayer.X;}
- }
- if (this.spelBord[i][i]==TicTacToePlayer.O){
- aantalO++;
- if (aantalO == 3) {return TicTacToePlayer.O;}
- }
- }
- aantalX = 0;
- aantalO = 0;
- // en de diagonaal van rechtsboven naar linksonder
- if (this.spelBord[2][0]==TicTacToePlayer.X) {aantalX++;}
- if (this.spelBord[2][0]==TicTacToePlayer.O) {aantalO++;}
- if (this.spelBord[1][1]==TicTacToePlayer.X) {aantalX++;}
- if (this.spelBord[1][1]==TicTacToePlayer.O) {aantalO++;}
- if (this.spelBord[0][2]==TicTacToePlayer.X) {aantalX++;}
- if (this.spelBord[0][2]==TicTacToePlayer.O) {aantalO++;}
- if (aantalX == 3) {return TicTacToePlayer.X;}
- if (aantalO == 3) {return TicTacToePlayer.O;}
- return null;
- }
- public boolean isEmpty(int row, int column){
- return this.spelBord[row-1][column-1] == null;
- }
- public boolean isFinished(){
- // eerst kijken of er een winnaar is (dat kan zonder dat spelbord volledig is.
- if (this.getWinner()!=null){
- return true;
- }
- // indien er nog een vakje is met een null , is het spel nog niet gedaan.
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- if (this.spelBord[i][j]==null){
- return false;
- }
- }
- }
- return true;
- }
- public boolean setCell(int row, int column){
- if (row>=1 && row<=3 && column>=1 &&column <=3) {
- if (this.spelBord[row-1][column-1]==null) {
- this.spelBord[row-1][column-1]=this.getCurrentTurn();
- return true;
- }
- }
- return false;
- }
- public String toString(){
- puzzelString = "╔═══╦═══╦═══╗"+ System.lineSeparator();
- for (int i = 0; i < 3; i++) {
- puzzelString = puzzelString + "║ ";
- for (int j = 0; j < 3; j++) {
- if (this.spelBord[i][j]==null) {
- puzzelString = puzzelString + " ";
- }else {
- puzzelString = puzzelString + this.spelBord[i][j];
- }
- if (j<2) {
- puzzelString = puzzelString + " ║ ";
- }
- }
- puzzelString = puzzelString + " ║"+ System.lineSeparator();
- if (i<2){ puzzelString = puzzelString + "╠═══╬═══╬═══╣" + System.lineSeparator();}
- }
- puzzelString = puzzelString + "╚═══╩═══╩═══╝";
- return puzzelString;
- }
- public static void main(String[] args) {
- String positie;
- String sRow;
- String sCol;
- int iCol;
- int iRow;
- TicTacToe newSpel = new TicTacToe();
- System.out.println(newSpel.toString());
- while (!newSpel.isFinished()){
- System.out.println("De beurt is aan " + newSpel.getCurrentTurn()+". Kies een vakje (rij,kolom).");
- Scanner keyboard = new Scanner(System.in);
- positie = keyboard.nextLine();
- String[] p = positie.split(",");
- // er zouden nooit meer dan 2 posities mogen zijn in deze array
- iRow = Integer.parseInt(p[0]);
- iCol = Integer.parseInt(p[1]);
- newSpel.setCell(iRow, iCol);
- System.out.println(newSpel.toString());
- }
- if (newSpel.getWinner()== null) {
- System.out.println("Gelijkspel!");
- } else {
- System.out.println(newSpel.getWinner() + " heeft gewonnen!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement