repl.it
@R_Gaspar/

Tic-Tac-Toe!

Java

No description

fork
loading
Files
  • Main.java
  • jdt.ls-java-project
  • GameFrame.java
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.lang.*;
class Main {
  public static void main(String[] args) {
	//Setup
	GameFrame TicTacToe = new GameFrame();
	GameFrame.clear_screen();
	GameFrame.GameSetup list_of_players = TicTacToe.new GameSetup(2);
	list_of_players.display_and_change();
	//Start Game Activity
	GameFrame.GameTime score_keep = TicTacToe.new GameTime(list_of_players.output_raw_list());
	boolean game_status = true;
	int prev_winner =0;
	int play_turn = 0;
	int games = 0;
	boolean tie_status=true;
	ArrayList<String> player = list_of_players.output_raw_list();
	TicTacToeGame.assign_users(player,score_keep.return_raw_map());
	
	do{
		if (games != 0 &&!tie_status) {
			System.out.printf("Previous Winner was %s.\n%s starts next.\033[?25l", player.get(prev_winner),player.get(prev_winner));
			try {
				Thread.sleep(2000);
				System.out.print("\033[?25h");
			} catch (InterruptedException a) {
				System.out.println("");
			}
		}else if (games!=0 && tie_status) {
			System.out.printf("Tie.\n%s starts next.\033[?25l",player.get(prev_winner));
			try {
				Thread.sleep(2000);
				System.out.print("\033[?25h");
			} catch (InterruptedException a) {
				System.out.println("");
			}
		}
		play_turn=prev_winner;
		for (int space=0;space<9;space++){
			TicTacToeGame.turn(play_turn);
			if (TicTacToeGame.check()!=-1){
				tie_status=false;
				GameFrame.clear_screen();
				TicTacToeGame.display_board();
				System.out.printf("%s is the Winner!\n",player.get(TicTacToeGame.check()));
				prev_winner = TicTacToeGame.check();
				score_keep.win_count(player.get(TicTacToeGame.check()));
				break;
			}
			play_turn= (play_turn+1)%2;
			if (space==8){
				GameFrame.clear_screen();
				TicTacToeGame.display_board();
				System.out.println("Tie!");
				prev_winner=(prev_winner+1)%2;
				tie_status=true;
			}
		}
		System.out.println(TicTacToeGame.display_scores(score_keep.return_raw_map()));
		game_status = score_keep.play_again();
		
		if(game_status){
			TicTacToeGame.reset();
			games++;
		}
	}while (game_status);
	
	
	//End of Game
	GameFrame.GameEnd ending = TicTacToe.new GameEnd(score_keep.return_raw_map(),list_of_players.output_raw_list() );
	ending.display_results();
	
  }
}
final class TicTacToeGame{
	private static ArrayList<String> player_names;
	private static HashMap<String,Integer> score_count;
	private static String[][] row = {{"#","#","#"},{"#","#","#"},{"#","#","#"}};
	private static String[] mark = {"\033[31mO\033[0m","\033[34mX\033[0m"};
	public static void game_title(){
		
	}
	public static void assign_users(ArrayList<String> users,HashMap<String,Integer> scores){
		player_names=users;
		score_count=scores;
	}
	private static Scanner boardInput=new Scanner (System.in);
	public static void turn(int play){
		//Scanner boardInput = new Scanner(System.in);
		boolean status;
		while (true){
			GameFrame.clear_screen();
			if (!error.equals("")){
			System.out.println(error);
			}
			display_board();
			System.out.printf("%s's Turn\n", player_names.get(play));
			System.out.println("Choose a square (LetterNumber format)");
			System.out.printf("\033[1B%s\033[3A",display_scores(score_count));
			String choice = boardInput.nextLine();
			status =selection(choice,play);
			if (status){
				GameFrame.clear_screen();
				break;
			}
		}
	}	
	public static void reset(){
		for (int a = 0;a<3; a++){
			for (int b=0;b<3;b++){
				row[a][b]="#";
			}
		}
	}

	public static void display_board(){
		System.out.println("# 1 2 3");
		for (int a = 0;a<3; a++){
			char letter = (char) (65+a);
			System.out.println(letter+" "+row[a][0]+"|"+row[a][1]+"|"+row[a][2]);
			if (a!=2){
				System.out.println("  -+-+-");
			}
		}
		System.out.println("");
	}
	public static String display_scores(HashMap<String,Integer> users){
		String blank = "          ";
		return String.format("%s: %d\n%s: %d\n",player_names.get(0).substring (0, (player_names.get(0).length()<10)?player_names.get(0).length():10) + blank.substring((player_names.get(0).length()<10)?player_names.get(0).length():10),users.get(player_names.get(0)) ,player_names.get(1).substring(0,(player_names.get(1).length()<10)?player_names.get(1).length():10)+blank.substring((player_names.get(1).length()<10)?player_names.get(1).length():10),users.get(player_names.get(1)) );
	}

	private static String error="";
	public static boolean selection(String choice,int player){
		if (choice.length() !=2){
			error= "Not a valid choice.";
			return false;
		}else {
			choice = choice.toUpperCase();
			int columnline;
			Character rowline = choice.charAt(0);
			int rowlinefinal = rowline.charValue()-64;
			if ( rowlinefinal < 1 || rowlinefinal >3){
				error="Letter not in range.";
				return false;
			}
			try {
				columnline = Integer.parseInt(choice.substring(1));
			}catch (NumberFormatException e) {
				error="Not a valid number.";
				return false;
			}
			if (columnline<1 || columnline>3){
				error="Number not in range.";
				return false;				
			}
			if (row[rowlinefinal-1][columnline-1].equals("#")){
				change(rowlinefinal-1,columnline-1,player);
				error="";
				return true;
			}

		}
		error="Please choose another square";
		return false;
	}
	private static void change(int rowline, int column, int player){
		row[rowline][column] = mark[player];
	}
	
	public static int check(){
		/* 
		#|0 1 2
		-+-----
		0|1 2 3
		1|4 5 6
		2|7 8 9
		*/
		if ( (row[0][0].equals(row[0][1])&&row[0][1].equals(row[0][2]) && !row[0][0].equals("#")) ){
			return Arrays.binarySearch(mark, row[0][0]);
		}
		if ( (row[0][0].equals(row[1][1])&&row[1][1].equals(row[2][2]) && !row[0][0].equals("#")) ){
			return Arrays.binarySearch(mark, row[0][0]);
		}
		if ( (row[0][0].equals(row[1][0])&&row[1][0].equals(row[2][0]) && !row[0][0].equals("#")) ){
			return Arrays.binarySearch(mark, row[0][0]);
		}
		if ( (row[1][0].equals(row[1][1])&&row[1][1].equals(row[1][2]) && !row[1][0].equals("#")) ){
			return Arrays.binarySearch(mark, row[1][0]);
		}
		if ( (row[2][0].equals(row[1][1])&&row[1][1].equals(row[0][2]) && !row[2][0].equals("#")) ){
			return Arrays.binarySearch(mark, row[2][0]);
		}
		if ( (row[2][0].equals(row[2][1])&&row[2][1].equals(row[2][2]) && !row[2][0].equals("#")) ){
			return Arrays.binarySearch(mark, row[2][0]);
		}
		if ( (row[0][1].equals(row[1][1])&&row[1][1].equals(row[2][1]) && !row[0][1].equals("#")) ){
			return Arrays.binarySearch(mark, row[0][1]);
		}
		if ( (row[0][2].equals(row[1][2])&&row[1][2].equals(row[2][2]) && !row[0][2].equals("#")) ){
			return Arrays.binarySearch(mark, row[0][2]);
		}
		return -1;
	}
}
?