From 3f81484cbaf0b07f471f468598c55332c5dd17f6 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Thu, 7 Oct 2021 17:38:50 +0200 Subject: [PATCH] changed return type boolean of Method setField javadoc added --- src/Gamefield.java | 124 ++++++++++++++++++++++++--------------------- src/TicTacToe.java | 5 +- 2 files changed, 69 insertions(+), 60 deletions(-) diff --git a/src/Gamefield.java b/src/Gamefield.java index 39cced8..5bf1da1 100644 --- a/src/Gamefield.java +++ b/src/Gamefield.java @@ -1,48 +1,59 @@ + + public class Gamefield { - private int field1, field2, field3, field4, field5, field6, field7, field8, field9; + private int field1 = 0, field2 = 0, field3 = 0, field4 = 0, field5 = 0, field6 = 0, field7 = 0, field8 = 0, field9 = 0; - Gamefield() { - field1 = 0; - field2 = 0; - field3 = 0; - field4 = 0; - field5 = 0; - field6 = 0; - field7 = 0; - field8 = 0; - field9 = 0; + /** + * Constructor of class Gamefield + */ + Gamefield() {} + + /** + * Method to set a field when it is set from player + * @param field the field which should be set. (1-9) + * @param player the player who sets the field (1 or 2) + * @return true --> field is set, false --> field isn't set because it's already set or it doesn't exist. + */ + public boolean setField(int field, int player) { + if(field == 1 && field1 == 0) { + field1 = player; + } + else if(field == 2 && field2 == 0) { + field2 = player; + } + else if(field == 3 && field3 == 0) { + field3 = player; + } + else if(field == 4 && field4 == 0) { + field4 = player; + } + else if(field == 5 && field5 == 0) { + field5 = player; + } + else if(field == 6 && field6 == 0) { + field6 = player; + } + else if(field == 7 && field7 == 0) { + field7 = player; + } + else if(field == 8 && field8 == 0) { + field8 = player; + } + else if(field == 9 && field9 == 0) { + field9 = player; + } + else { + return false; //if field is already set or it doesn't exist. + } + return true; //if change is done } - public void setField(int field, int player) { - if(field == 1) { - field1 = player; - } - else if(field == 2) { - field2 = player; - } - else if(field == 3) { - field3 = player; - } - else if(field == 4) { - field4 = player; - } - else if(field == 5) { - field5 = player; - } - else if(field == 6) { - field6 = player; - } - else if(field == 7) { - field7 = player; - } - else if(field == 8) { - field8 = player; - } - else if(field == 9) { - field9 = player; - } - } + /** + * + * @param field the number of the requested field + * @return integer of the player, which has set the requested field (1 -> Player 1, 2 -> Player 2, -1 --> the requested field doens't exist.) + */ public int getField(int field) { if(field == 1) { return field1; @@ -71,30 +82,25 @@ public class Gamefield { else if(field == 9) { return field9; } - return -1; + return -1; //if the requested field doesn't exist, -1 will be returned. } + /** + * Method to print field + */ public void outputField() { - System.out.print(getSymbol(field1)); - System.out.print("|"); - System.out.print(getSymbol(field2)); - System.out.print("|"); - System.out.println(getSymbol(field3)); - System.out.println("_____"); - System.out.print(getSymbol(field4)); - System.out.print("|"); - System.out.print(getSymbol(field5)); - System.out.print("|"); - System.out.println(getSymbol(field6)); - System.out.println("_____"); - System.out.print(getSymbol(field7)); - System.out.print("|"); - System.out.print(getSymbol(field8)); - System.out.print("|"); - System.out.println(getSymbol(field9)); - + System.out.println(getSymbol(field1) + "|" + getSymbol(field2) + "|" + getSymbol(field3)); + System.out.println("-----"); + System.out.println(getSymbol(field4) + "|" + getSymbol(field5) + "|" + getSymbol(field6)); + System.out.println("-----"); + System.out.println(getSymbol(field7) + "|" + getSymbol(field8) + "|" + getSymbol(field9)); } + /** + * + * @param player number of player who has set the field (1 or 2) + * @return the Symbol of the player who has set the field (X or O) + */ private String getSymbol(int player) { if(player == 1) { return "X"; diff --git a/src/TicTacToe.java b/src/TicTacToe.java index 263920f..9fe0ac3 100644 --- a/src/TicTacToe.java +++ b/src/TicTacToe.java @@ -1,7 +1,10 @@ public class TicTacToe { public static void main(String[] args) { - System.out.println("test"); + Gamefield gamefield = new Gamefield(); + gamefield.setField(1, 1); + gamefield.setField(7, 2); + gamefield.outputField(); } public TicTacToe(String int1, int int2) {