11package connecta4 ;
22
33public class Game {
4+ public static final String RESET = "\u001B [0m" ;
5+ public static final String BLACK = "\u001B [30m" ;
6+ public static final String RED = "\u001B [31m" ;
7+ public static final String GREEN = "\u001B [32m" ;
8+ public static final String YELLOW = "\u001B [33m" ;
9+ public static final String BLUE = "\u001B [34m" ;
10+ public static final String PURPLE = "\u001B [35m" ;
11+ public static final String CYAN = "\u001B [36m" ;
12+ public static final String WHITE = "\u001B [37m" ;
13+
414 Player player1 ;
515 Player player2 ;
616 Table main_Table ;
@@ -38,7 +48,7 @@ public void setMain_Table(Table main_Table) {
3848 }
3949
4050 public boolean remakeGame (String confirmation ) {
41-
51+
4252 if (confirmation .toUpperCase () == "Y" ) {
4353 return true ;
4454 } else {
@@ -53,33 +63,33 @@ public boolean remakeGame(String confirmation) {
5363 */
5464 public void printTable (char [][] taulell ) {
5565
56- System .out .println (" " + "--" .repeat (taulell [0 ].length ));
66+ System .out .println (PURPLE + " " + "--" .repeat (taulell [0 ].length ) + RESET );
5767 for (int row = 0 ; row < taulell .length ; row ++) {
58- System .out .print ((row + 1 ) + " |" );
68+ System .out .print (GREEN + (row + 1 ) + " |" );
5969 for (int col = 0 ; col < taulell [0 ].length ; col ++) {
60- System .out .print (taulell [row ][col ]);
61- System .out .print ("|" );
70+ System .out .print (taulell [row ][col ] + GREEN );
71+ System .out .print ("|" + GREEN );
6272 }
63- System .out .println ();
64- System .out .println (" " + ("--" .repeat (taulell [0 ].length )));
73+ System .out .println (RESET );
74+ System .out .println (PURPLE + " " + ("--" .repeat (taulell [0 ].length ) + RESET ));
6575 }
6676 System .out .print (" " );
6777 for (int i = 1 ; i <= taulell [0 ].length ; i ++) {
68- System .out .print (" " + i );
78+ System .out .print (GREEN + " " + i );
6979 }
70- System .out .println ();
80+ System .out .println (RESET );
81+
7182 }
7283
73-
7484 public void tirada (char [][] taulell , int columna ) {
7585 columna -= 1 ;
7686 this .turns ++;
7787 boolean torbat = false ;
7888 // Recorro de forma recursiva per comprobar primer l'ultim index
79- do {
80-
89+ do {
90+
8191 for (int i = taulell .length - 1 ; i >= 0 ; i --) {
82- if (torbat ){
92+ if (torbat ) {
8393 break ;
8494 }
8595 for (int j = 0 ; j < taulell .length ; j ++) {
@@ -89,31 +99,73 @@ public void tirada(char[][] taulell, int columna) {
8999 }
90100 }
91101 }
92- }while (!torbat );
93-
102+ } while (!torbat );
103+
94104 }
95- public boolean comprobarVictoria (){
96- boolean victoria = false ;
97- int matches = 0 ;
98- for (int i = 0 ; i < this .main_Table .grid .length ; i ++) {
99- for (int j = 0 ; j < this .main_Table .grid [0 ].length ; j ++) {
100- if (this .main_Table .grid [i ][j ] == asignFitxa ()) {
101- matches ++;
102- if (matches == 4 ){
103- victoria = true ;
104- }
105+
106+ public boolean comprovaVictoria () {
107+ char jugador = asignFitxa ();
108+ int files = this .main_Table .grid .length ;
109+ int columnes = this .main_Table .grid [0 ].length ;
110+
111+ // Comprovar files
112+ for (int i = 0 ; i < files ; i ++) {
113+ for (int j = 0 ; j < columnes - 3 ; j ++) { // -3 per no sortir del rang
114+ if (this .main_Table .grid [i ][j ] == jugador &&
115+ this .main_Table .grid [i ][j + 1 ] == jugador &&
116+ this .main_Table .grid [i ][j + 2 ] == jugador &&
117+ this .main_Table .grid [i ][j + 3 ] == jugador ) {
118+ return true ;
105119 }
106120 }
121+ }
107122
123+ // Comprovar columnes
124+ for (int i = 0 ; i < files - 3 ; i ++) { // -3 per no sortir del rang
125+ for (int j = 0 ; j < columnes ; j ++) {
126+ if (this .main_Table .grid [i ][j ] == jugador &&
127+ this .main_Table .grid [i + 1 ][j ] == jugador &&
128+ this .main_Table .grid [i + 2 ][j ] == jugador &&
129+ this .main_Table .grid [i + 3 ][j ] == jugador ) {
130+ return true ;
131+ }
132+ }
108133 }
109- return victoria ;
134+
135+ // Comprovar diagonal principal (de dalt a baix)
136+ for (int i = 0 ; i < files - 3 ; i ++) {
137+ for (int j = 0 ; j < columnes - 3 ; j ++) {
138+ if (this .main_Table .grid [i ][j ] == jugador &&
139+ this .main_Table .grid [i + 1 ][j + 1 ] == jugador &&
140+ this .main_Table .grid [i + 2 ][j + 2 ] == jugador &&
141+ this .main_Table .grid [i + 3 ][j + 3 ] == jugador ) {
142+ return true ;
143+ }
144+ }
145+ }
146+
147+ // Comprovar diagonal inversa (de baix a dalt)
148+ for (int i = 3 ; i < files ; i ++) { // Comença des de 3 per evitar sortir del rang
149+ for (int j = 0 ; j < columnes - 3 ; j ++) {
150+ if (this .main_Table .grid [i ][j ] == jugador &&
151+ this .main_Table .grid [i - 1 ][j + 1 ] == jugador &&
152+ this .main_Table .grid [i - 2 ][j + 2 ] == jugador &&
153+ this .main_Table .grid [i - 3 ][j + 3 ] == jugador ) {
154+ return true ;
155+ }
156+ }
157+ }
158+
159+ return false ; // No hi ha 4 en ratlla
110160 }
111-
112-
113- /**Metode per assignar un caracter cada torn, si es parell asigna un i si és imparell un altre
114- * @return Un char
161+
162+ /**
163+ * Metode per assignar un caracter cada torn, si es parell asigna un i si és
164+ * imparell un altre
165+ *
166+ * @return Un char
115167 */
116- public char asignFitxa () {
168+ public char asignFitxa () {
117169 char fitxa ;
118170 if (this .turns % 2 == 0 ) {
119171 fitxa = 'X' ;
0 commit comments