Last active
January 2, 2017 17:36
-
-
Save GarryOne/f8b5d7c9a650d120835790aeb38369ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import java.util.Random; | |
public class Shindra { | |
public static int NR_CARDS = 9; | |
public static int [][] tab = new int[4][4]; | |
public static int [][] _humanTab = new int[4][4]; | |
public static int [][] _computerTab = new int[4][4]; | |
public static int [] _humanCards = new int[9]; | |
public static int [] _computerCards = new int[9]; | |
public static int strategy = 0; | |
public static int [][] _possibleCards = new int [][] { | |
{2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 8, 9, 9, 10, 10}, // strategy 0 | |
{5, 5, 6, 6, 7, 8, 8, 9, 9, 10, 10} // strategy 1 | |
}; | |
public static void showTab(int tab[][]){ | |
for(int i = 0;i < 4;i++){ | |
System.out.println("+-----------------------+"); | |
for(int j = 0;j < 4;j++){ | |
System.out.print("|"); | |
if(_humanTab[i][j] > 0) { | |
System.out.print(" ("+ _humanTab[i][j] +") "); // if human card, sorround with round brackets | |
}else if(_computerTab[i][j] > 0){ | |
System.out.print(" ["+_computerTab[i][j]+"] "); // if computer card, sorround with square brackets | |
} else { | |
System.out.print(" "+tab[i][j]+" "); | |
} | |
} | |
System.out.print("|"); | |
System.out.println(); | |
} | |
System.out.println("+-----------------------+"); | |
} | |
public static int checkArray(int [] a, int value) { | |
for(int i = 0; i < a.length; i++) { | |
if(a[i] == value) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
public static int getRandom(int array []) { | |
int min = 0; | |
int max = array.length - 1; | |
Random r = new Random(); | |
int rnd_index = r.nextInt((max - min) + 1) + min; | |
return array[rnd_index]; | |
} | |
public static int[] removeElement(int[] original, int element){ | |
int[] n = new int[original.length - 1]; | |
System.arraycopy(original, 0, n, 0, element ); | |
System.arraycopy(original, element+1, n, element, original.length - element-1); | |
return n; | |
} | |
public static void showArray(int [] a) { | |
for(int i = 0; i < a.length; i++) { | |
System.out.print(a[i] + " "); | |
} | |
System.out.println(); | |
} | |
public static float abs(float a) { | |
return (a <= 0.0F) ? 0.0F - a : a; | |
} | |
public static boolean hasNeighbours(int [][] tab, int [][] yourTab, int i, int j) { | |
if( (i > 0 && tab[i-1][j] > 0 && tab[i-1][j] == yourTab[i-1][j]) || // top | |
(i < tab.length - 1 && tab[i+1][j] > 0 && tab[i+1][j] == yourTab[i+1][j]) || // top | |
(j < tab.length - 1 && tab[i][j+1] > 0 && tab[i][j+1] == yourTab[i][j+1]) || // right | |
(j > 0 && tab[i][j-1] > 0 && tab[i][j-1] == yourTab[i][j-1]) // left | |
) { | |
return true; | |
} | |
return false; | |
} | |
public static boolean checkMatrixFilled(int [][] matrix) { | |
for (int i = 0; i < tab.length; i++) { | |
for (int j = 0; j < tab.length; j++) { | |
if(matrix[i][j] == 0) { | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
public static void chooseHumanCards(){ | |
int [] possibleCards = _possibleCards[0]; // all possible cards | |
Scanner sc = new Scanner(System.in); | |
for(int i = 0; i < NR_CARDS; i++) { | |
System.out.println("Choose card "+(i+1)+":"); | |
int card = sc.nextInt(); | |
int found = checkArray(possibleCards, card); | |
if(found >= 0){ | |
_humanCards[i] = card; | |
possibleCards = removeElement(possibleCards, found); | |
} else { | |
i--; | |
System.out.println("Card doesn't exists"); | |
} | |
} | |
} | |
public static void chooseComputerCards(){ | |
int [] possibleCards = _possibleCards[strategy]; | |
for(int i = 0; i < NR_CARDS; i++) { | |
int card = getRandom(possibleCards); | |
int rnd_index = checkArray(possibleCards, card); | |
possibleCards = removeElement(possibleCards, rnd_index); | |
_computerCards[i] = card; | |
} | |
} | |
public static void chooseHumanPos() { | |
Scanner sc = new Scanner(System.in); | |
int i, j; | |
do { | |
System.out.println("Choose position:"); | |
System.out.print("row: "); | |
i = sc.nextInt() - 1; // decrement for array use | |
System.out.print("column: "); | |
j = sc.nextInt() - 1; // decrement for array use | |
} while( (i < 0 || i > tab.length - 1) || (j < 0 || j > tab.length - 1) || tab[i][j] != 0 | |
|| (!hasNeighbours(tab, _computerTab, i, j) && (_humanCards.length < NR_CARDS || _computerCards.length < NR_CARDS))); | |
int card; | |
do { | |
System.out.println("Choose card:"); | |
card = sc.nextInt(); | |
} while(checkArray(_humanCards, card) == -1); | |
tab[i][j] = card; | |
_humanTab[i][j] = card; | |
_humanCards = removeElement(_humanCards, checkArray(_humanCards, card)); | |
checkNeighboursToConquer(card, _humanTab, _computerTab, i, j, true); | |
} | |
public static void chooseComputerPos(){ | |
int _i = -1, _j = -1; | |
int card; | |
int best_card = -1; | |
int max_points = 0; | |
int points; | |
if(_humanCards.length == NR_CARDS && _computerCards.length == NR_CARDS) { | |
_i = (int) (Math.random()*3); | |
_j = (int) (Math.random()*3); | |
} else { | |
for(int i = 0; i < tab.length; i++) { | |
for(int j = 0; j < tab.length; j++) { | |
for(int k = 0; k < _computerCards.length; k++) { | |
if(tab[i][j] == 0 && hasNeighbours(tab, _humanTab, i, j)) { // if there is no card and it's a valid pos | |
card = _computerCards[k]; | |
points = checkNeighboursToConquer(card, _computerTab, _humanTab, i, j, false); | |
if(points > max_points) { // store new card and position | |
max_points = points; | |
best_card = card; | |
_i = i; | |
_j = j; | |
} | |
} | |
} | |
} | |
} | |
} | |
card = best_card; | |
tab[_i][_j] = card; | |
// showArray(_computerCards); | |
_computerTab[_i][_j] = card; | |
_computerCards = removeElement(_computerCards, checkArray(_computerCards, card)); | |
checkNeighboursToConquer(card, _computerTab, _humanTab, _i, _j, true); | |
} | |
public static int checkNeighboursToConquer(int card, int [][] myTab, int [][] yourTab, int _i, int _j, boolean conquer) { | |
if(_i < 0 || _i > tab.length || _j < 0 || _j > tab.length ) { | |
return 0; | |
} | |
// If conquer == true, we start to count points based on how good position is | |
int points = 0; | |
// Check for Viruses | |
int [][][] possibleViruses = { | |
{{_i - 1, _j}, {_i, _j + 1}}, // top - right | |
{{_i + 1, _j}, {_i, _j + 1}}, // right - bottom | |
{{_i + 1, _j}, {_i, _j - 1}}, // bottom - left | |
{{_i - 1, _j}, {_i, _j - 1}} // left - top | |
}; | |
boolean [] conditionsForViruses = { | |
_i > 0 && _j < tab.length - 1, // top - right | |
_i < tab.length - 1 && _j < tab.length - 1, // right - bottom | |
_i < tab.length - 1 && _j > 0, // bottom - left | |
_i > 0 && _j > 0 // left - top | |
}; | |
for(int i = 0; i < possibleViruses.length; i++) { | |
int c1_i = possibleViruses[i][0][0]; | |
int c1_j = possibleViruses[i][0][1]; | |
int c2_i = possibleViruses[i][1][0]; | |
int c2_j = possibleViruses[i][1][1]; | |
if(conditionsForViruses[i] && tab[c1_i][c1_j] > 0 && tab[c2_i][c2_j] > 0 && tab[c1_i][c1_j] == yourTab[c1_i][c1_j] && tab[c2_i][c2_j] == yourTab[c2_i][c2_j] | |
&& abs(tab[c1_i][c1_j] - tab[c2_i][c2_j]) == card) { // if difference between two opponent cards is value of your nearby card | |
if (conquer == true) { | |
myTab[c1_i][c1_j] = yourTab[c1_i][c1_j]; | |
myTab[c2_i][c2_j] = yourTab[c2_i][c2_j]; | |
yourTab[c1_i][c1_j] = 0; | |
yourTab[c2_i][c2_j] = 0; | |
checkNeighboursToConquer(myTab[c1_i][c1_j], myTab, yourTab, c1_i, c1_j, conquer); | |
checkNeighboursToConquer(myTab[c2_i][c2_j], myTab, yourTab, c2_i, c2_j, conquer); | |
} else { | |
points += 3; | |
} | |
} | |
} | |
// top | |
if(_i > 0 && tab[_i-1][_j] > 0 && tab[_i-1][_j] == yourTab[_i-1][_j] && card > yourTab[_i-1][_j]) { | |
if (conquer == true) { | |
myTab[_i-1][_j] = yourTab[_i-1][_j]; | |
yourTab[_i-1][_j] = 0; | |
card = myTab[_i-1][_j]; | |
checkNeighboursToConquer(card, myTab, yourTab, _i-1, _j, conquer); | |
} else { | |
points += 1; | |
} | |
} | |
// bottom | |
if(_i < tab.length - 1 && tab[_i+1][_j] > 0 && tab[_i+1][_j] == yourTab[_i+1][_j] && card > yourTab[_i+1][_j]) { | |
if (conquer == true) { | |
myTab[_i+1][_j] = yourTab[_i+1][_j]; | |
yourTab[_i+1][_j] = 0; | |
card = myTab[_i+1][_j]; | |
checkNeighboursToConquer(card, myTab, yourTab, _i+1, _j, conquer); | |
} else { | |
points += 1; | |
} | |
} | |
// right | |
if(_j < tab.length - 1 && tab[_i][_j+1] > 0 && tab[_i][_j+1] == yourTab[_i][_j+1] && card > yourTab[_i][_j+1]) { | |
if (conquer == true) { | |
myTab[_i][_j+1] = yourTab[_i][_j+1]; | |
yourTab[_i][_j+1] = 0; | |
card = myTab[_i][_j+1]; | |
checkNeighboursToConquer(card, myTab, yourTab, _i, _j+1, conquer); | |
} else { | |
points += 1; | |
} | |
} | |
// left | |
if(_j > 0 && tab[_i][_j-1] > 0 && tab[_i][_j-1] == yourTab[_i][_j-1] && card > yourTab[_i][_j-1]) { | |
if (conquer == true) { | |
myTab[_i][_j-1] = yourTab[_i][_j-1]; | |
yourTab[_i][_j-1] = 0; | |
card = myTab[_i][_j-1]; | |
checkNeighboursToConquer(card, myTab, yourTab, _i, _j-1, conquer); | |
} else { | |
points += 1; | |
} | |
} | |
return points; | |
} | |
public static void main(String[] args) { | |
chooseHumanCards(); | |
System.out.println("Your cards: "); | |
showArray(_humanCards); | |
chooseComputerCards(); | |
while(!checkMatrixFilled(tab)) { | |
chooseHumanPos(); | |
showTab(tab); | |
chooseComputerPos(); | |
showTab(tab); | |
System.out.println("Your cards: "); | |
showArray(_humanCards); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment