-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
141 lines (132 loc) · 6.22 KB
/
Main.java
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
package com.company.TestCourse;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
//private String[][] board = new String[6][7];
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Board test = new Board();
System.out.print("Enter the name of the Red Player: ");
RedPlayer player1 = new RedPlayer(input.next());
System.out.print("Enter the name of the Yellow Player: ");
YellowPlayer player2 = new YellowPlayer(input.next());
boolean playComplete = false;
int numOfPlays = 0;
int maxNumOfPlays = test.getBoard().length * (test.getBoard()[0].length-1);
//System.out.println(maxNumOfPlays);
// System.out.println(test.getBoard()[0].length);
// System.out.println(test.getBoard().length);
while(numOfPlays < maxNumOfPlays && test.hasWon() == false){
if(numOfPlays >= maxNumOfPlays){
//System.out.println("There has been a draw");
test.setWon(true);
}
playComplete = false;
while(playComplete == false && test.hasWon() == false) {
if (numOfPlays % 2 == 0) {
try {
System.out.print(player1.getName() + ", Enter in the Column to which you want to put your Red piece! (1-7)");
int col = input.nextInt();
if(!(test.getBoard()[0][col - 1].equals(" "))){
throw new columnFullException(col);
}
if(col == 8){
throw new IndexOutOfBoundsException();
}
for (int x = test.getBoard().length - 1; x >= 0; x--) {
if (test.getBoard()[x][col - 1].equals(" ")) {
test.getBoard()[x][col - 1] = "R";
playComplete = true;
test.refreshBoard();
numOfPlays++;
System.out.println("Total number of plays: "+numOfPlays);
test.diagLeft("R", x, col - 1);
test.diagRight("R", x, col - 1);
test.upDown("R", x, col - 1);
test.checkSides("R", x, col - 1);
break;
}
}
}
catch (IndexOutOfBoundsException x) {
System.out.println("Choose another column, this one is unavailable!!!");
input.nextLine();
}
catch (InputMismatchException x){
System.out.println("Please enter reenter the position as an Integer!!");
input.nextLine();
}
catch(columnFullException x){
System.out.print(x.getMessage());
input.nextLine();
}
}
if (numOfPlays % 2 == 1 && test.hasWon() == false) {
try {
System.out.print(player2.getName() + ", Enter in the Column to which you want to put your Yellow piece! (1-7)");
int col2 = input.nextInt();
if(!(test.getBoard()[0][col2 - 1].equals(" "))){
throw new columnFullException(col2);
}
if(col2 == 8){
throw new IndexOutOfBoundsException();
}
for (int x = test.getBoard().length - 1; x >= 0; x--) {
if (test.getBoard()[x][col2 - 1].equals(" ")) {
test.getBoard()[x][col2 - 1] = "Y";
playComplete = true;
test.refreshBoard();
numOfPlays++;
System.out.println("Total number of play: "+numOfPlays);
test.diagLeft("Y", x, col2 - 1);
test.diagRight("Y", x, col2 - 1);
test.upDown("Y", x, col2 - 1);
test.checkSides("Y", x, col2 - 1);
break;
}
}
}
catch (IndexOutOfBoundsException x) {
System.out.println("Choose another column, this one is unavailable!!!");
input.nextLine();
}
catch(InputMismatchException x){
System.out.println("Please enter reenter the position as an Integer!!");
input.nextLine();
}
catch(columnFullException x){
System.out.println(x.getMessage());
input.nextLine();
}
}
}
}
if(numOfPlays == maxNumOfPlays){
System.out.println("There has been a draw!!");
}
else if(numOfPlays % 2 == 0){
player1.win();
player2.lose();
System.out.println(player1.getOutcome());
System.out.println(player2.getOutcome());
//System.out.print(numOfPlays);
}
else{
player1.lose();
player2.win();
System.out.println(player1.getOutcome());
System.out.print(player2.getOutcome());
//System.out.print(numOfPlays);
}
}
}
class columnFullException extends Exception {
private int colNum;
public columnFullException(int colNum){
super("Column "+colNum+" is full. Please select another column!!!\n");
this.colNum = colNum;
}
public int getColNum(){
return this.colNum;
}
}