-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXando.java
More file actions
160 lines (142 loc) · 4.79 KB
/
Xando.java
File metadata and controls
160 lines (142 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import java.util.Scanner;
class Xando{
private String[][] board = new String[3][3];
private boolean running = true;
private int playerTurn = 1;
private Scanner scanner = new Scanner(System.in);
/**
* Function initialises the program
*/
public static void main(String[] args){
Xando instance = new Xando();
instance.start();
}
/**
* Start function to run the program
*/
public void start(){
this.clearBoard();
this.printBoard();
while(this.running){
this.tick();
}
}
/**
* This function controls the main logic of the program
*/
private void tick(){
System.out.println("Player's " + this.playerTurn + " turn: input coordinates to go to");
String[] coordinates = this.scanner.nextLine().split(" "); //Splits coordinates into array so we can work with that
if(coordinates.length != 2){ //some validation
return;
}
String entry;
if(this.playerTurn == 1){
entry = "X";
}else{
entry = "O";
}
//following block of code is used to make sure that the coordinates are valid
int x = 0;
int y = 0;
try{
y = Integer.parseInt(coordinates[0]);
x = Integer.parseInt(coordinates[1]);
}catch(Exception e){
System.out.println("Not valid position to move");
return;
}
if(!this.processTurn(x, y, entry)){ //handles the turn
return;
}
if(this.playerTurn == 1){ //alternates between two players
this.playerTurn = 2;
}else{
this.playerTurn = 1;
}
this.printBoard(); //updates the board after each turn so players can se updates board
}
/**
* Handles every turn where player goes.
* Returns boolean that represents if the turn is valid and can happen or no.
*/
private boolean processTurn(int x, int y, String entry){
if(x > 2 || y > 2 || x < 0 || y < 0){
System.out.println("Not valid position to move");
return false;
}
if(!this.board[x][y].equals("-")){
System.out.println("Not valid position to move");
return false;
}
this.board[x][y] = entry;
if(this.checkForWin(x, y)){ //checks if one of the players has won
return false;
}
return true;
}
/**
* Called when player given in parameters has won
*/
private void celebrateWin(String player){
this.printBoard();
if(player.equals("X")){
System.out.println("Player 1 won");
}else{
System.out.println("Player 2 won");
}
this.running = false;
}
/**
* Checks all position near the given position and compares the entry in them. Calls celebrateWin function when 3 of the same entry is in a row.
* Returns boolean that depends if the win combinationb has been found or not.
*/
private boolean checkForWin(int x, int y){
String searchFor = this.board[x][y];
//The following block of code checks for every possible combinations to win
if(
this.board[0][0].equals(searchFor) && this.board[0][1].equals(searchFor) && this.board[0][2].equals(searchFor)
|| this.board[1][0].equals(searchFor) && this.board[1][1].equals(searchFor) && this.board[1][2].equals(searchFor)
|| this.board[2][0].equals(searchFor) && this.board[2][1].equals(searchFor) && this.board[2][2].equals(searchFor)
|| this.board[0][0].equals(searchFor) && this.board[1][0].equals(searchFor) && this.board[2][0].equals(searchFor)
|| this.board[0][1].equals(searchFor) && this.board[1][1].equals(searchFor) && this.board[2][1].equals(searchFor)
|| this.board[0][2].equals(searchFor) && this.board[1][2].equals(searchFor) && this.board[2][2].equals(searchFor)
|| this.board[0][0].equals(searchFor) && this.board[1][1].equals(searchFor) && this.board[2][2].equals(searchFor)
|| this.board[0][2].equals(searchFor) && this.board[1][1].equals(searchFor) && this.board[2][0].equals(searchFor)
){
this.celebrateWin(searchFor);
return true;
}
return false;
}
/**
* Clears the board/grid
*/
private void clearBoard(){
for(int x = 0; x < this.board.length; x++){
for(int y = 0; y < this.board[x].length; y++){
this.board[x][y] = "-";
}
}
}
/**
* Outputs the board/grid
*/
private void printBoard(){
for(int i = 0; i < 20; i++){
System.out.println();
}
String element;
for(int x = 0; x < this.board.length; x++){
System.out.print("___________\n");
for(int y = 0; y < this.board[x].length; y++){
element = this.board[x][y];
if(element == "-"){
element = " ";
}
System.out.print(element + " | ");
}
System.out.println();
}
}
}