|
// IVE Lab 11-1 Question 2 about Tic-Tac-Tao game
import javax.swing.*;
public class Lab_11_1_2 {
static String[] game_arr;
static int[] input_num_ar;
static int postion;
static String pc_token = "";
static String user_token = "";
static String result = "";
static String winner = "";
static final String HELP = "This is a Tic-Tac-Tao Game. Designed By Stephen Chung.\nIf you have any question, please to contact me.";
public static void main(String[] args) {
game_arr = new String[9];
input_num_ar = new int[2];
for (int i = 0; i < 9; i++) {
game_arr = " - ";
}
int confirm = 2;
while (confirm == 2) {
Object[] options = { "Cycle(O)", "Cross(X)", "HELP" };
confirm = JOptionPane.showOptionDialog(null, "Choose Your Token. Cycle(O) Will Be Start First", "CONFIRM MESSAGE",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
null, options, options[0]);
if (confirm == 2) {
JOptionPane.showMessageDialog(null, HELP);
continue;
}
}
for (int i = 0; i < 9; i++) {
if (confirm == 0) {
user_token = "O";
pc_token = "X";
} else {
user_token = "X";
pc_token = "O";
}
if ((i % 2 == 0 && confirm == 0) || (i % 2 != 0 && confirm == 1)) {
String ColRow = "";
String Step = "";
for (int ii = 0; ii < 2; ii++) {
switch (ii) {
case 0 : ColRow = "Col"; Step = "Step One";
break;
case 1 : ColRow = "Row"; Step = "Step Two";
break;
}
input_num_ar[ii] = showInputDialog("Input The " + ColRow +" Number For " + user_token + " : \n(" + Step + ")");
if (input_num_ar[ii] == -1) {
JOptionPane.showMessageDialog(null, " Row\n 1 2 3\n" + result + "\n" + "The Winner Is " + winner);
return;
} else if (input_num_ar[ii] == -10) {
return;
} else if (input_num_ar[ii] == -2) {
ii--;
JOptionPane.showMessageDialog(null, "Please Input A Integer Number");
continue;
} else if (input_num_ar[ii] == -3) {
ii--;
JOptionPane.showMessageDialog(null, "Please Input The Right Row and Col Number");
continue;
} else if (input_num_ar[ii] == -4) {
ii--;
JOptionPane.showMessageDialog(null, "Please Input A Integer Number");
continue;
}
}
postion = GetPlayerPos(input_num_ar[0], input_num_ar[1]);
if (postion >= 0 && postion <= 8) {
if (game_arr[postion] == " - ") {
game_arr[postion] = user_token;
} else {
i--;
JOptionPane.showMessageDialog(null, "Please Don't Input The Same Postion");
continue;
}
} else if (postion == -1) {
JOptionPane.showMessageDialog(null, "Please Input The Number of Row and Col with the range 1 - 3");
i--;
continue;
}
} else {
postion = GetPCPos();
if (game_arr[postion] == " - ") {
game_arr[postion] = pc_token;
}else {
i--;
continue;
}
}
}
int final_result = showInputDialog("CHECK RESULT");
if (final_result == -1) {
JOptionPane.showMessageDialog(null, " Row\n 1 2 3\n" + result + "\n" + "The Winner Is " + winner);
return;
} else {
JOptionPane.showMessageDialog(null, " Row\n 1 2 3\n" + result + "\n" + "The Game is Draw");
}
}
public static int GetPCPos() {
int x = 0;
for (int i = 0; i < 1; i++) {
x = (int)(Math.random() * 10);
if (x < 0 || x > 8) {
i--;
continue;
}
}
return x;
}
public static int showInputDialog(String args) {
int y = 0;
result = "";
for (int i = 0; i < game_arr.length; i++) {
if(i % 3 == 0 && i != 0) {
result += "\n";
}
if (i == 0) {
result += " " + (i + 1) + " " + game_arr + " ";
} else if (i == 3) {
result += "Col " + (i - 1) + " " + game_arr + " ";
} else if (i == 6) {
result += " " + (i - 3) + " " + game_arr + " ";
} else {
result += game_arr + " ";
}
}
winner = checkwin();
if (winner == "" && args != "CHECK RESULT") {
String x = JOptionPane.showInputDialog(null, " Row\n 1 2 3\n" + result + "\n" + args);
if (x == null) {
JOptionPane.showMessageDialog(null, "Click OK To Exit");
return -10;
} else if (x.length() == 0) {
return -2;
} else if (x.length() > 1 ) {
return -3;
}
int c_num;
for (int i = 0; i < x.length(); i++) {
c_num = (int) x.charAt(i);
if (c_num < 48 || c_num > 57) {
return -4;
}
}
y = Integer.parseInt(x);
} else if (winner != "") {
y = -1;
}
return y;
}
public static int GetPlayerPos(int x, int y) {
int z = -1;
if (x == 1) {
if (y == 1) {
z = 0;
} else if (y == 2) {
z = 1;
} else if (y == 3) {
z = 2;
}
} else if ( x == 2) {
if ( y == 1) {
z = 3;
} else if (y == 2) {
z = 4;
} else if (y == 3) {
z = 5;
}
} else if (x == 3) {
if (y == 1) {
z = 6;
} else if (y == 2) {
z = 7;
} else if (y ==3) {
z = 8;
}
} else {
return -1;
}
return z;
}
public static String checkwin() {
String i = "";
if (game_arr[0] == game_arr[1] && game_arr[1] == game_arr[2] && game_arr[0] != " - ") {
i = game_arr[0];
} else if (game_arr[0] == game_arr[3] && game_arr[3] == game_arr[6] && game_arr[0] != " - ") {
i = game_arr[0];
} else if (game_arr[0] == game_arr[4] && game_arr[4] == game_arr[8] && game_arr[0] != " - ") {
i = game_arr[0];
} else if (game_arr[1] == game_arr[4] && game_arr[4] == game_arr[7] && game_arr[1] != " - ") {
i = game_arr[1];
} else if (game_arr[2] == game_arr[4] && game_arr[4] == game_arr[6] && game_arr[2] != " - ") {
i = game_arr[2];
} else if (game_arr[2] == game_arr[5] && game_arr[5] == game_arr[8] && game_arr[2] != " - ") {
i = game_arr[2];
} else if (game_arr[3] == game_arr[4] && game_arr[4] == game_arr[5] && game_arr[3] != " - ") {
i = game_arr[3];
} else if (game_arr[6] == game_arr[7] && game_arr[7] == game_arr[8] && game_arr[6] != " - ") {
i = game_arr[6];
}
return i;
}
}
|
|