You are on page 1of 14

‫التغييرات‬

‫ملف ‪Bot.java‬‬

‫قبل‪:‬‬
‫ تحديث ما بعد التعديالت‬: ‫بعد‬
wildlife ‫ الخاص ب ال‬score ‫ للحصول على ال‬botstratgy ‫تم مالئمة هذا الكود الستخدام الميثود من‬
tile ‫و‬
nature token strategy ‫من الموجود أمام المستخدم وذلك ليتم اختيار األفضل منها وحساب ال‬
BoardTile tileChoice = null;
WildLifeToken tokenChoice = null;
boolean choicePicked = false;
//if user has a nature token, they are given options with selecting tiles and tokens

int[] tilesScores = BotStrategy.simulateTilePlacements(fourBoardTiles, bots, i, box,


bots.get(i).getBoard());
int[] tokensScores = BotStrategy.simulateTokenPlacements(fourWildLifeTokens, bots.get(i),
box, bots.get(i).getBoard());

if (bots.get(i).getNumNatureTokens() > 0) {
if (view.askBotUseToken(tilesScores, tokensScores)) {
view.displayBotChoice(bots.get(i), "to use a nature token.");

//if they want to use a token, display options

int option = view.askBotTokenUseOptions(tilesScores, tokensScores);


switch (option) {
case 1 -> {
//a) Take any one tile and one token from the four pairs
//ask user which tile and token they want
int[] chosenTileAndToken = view.UseBotTokenOption1(tilesScores,
tokensScores);
tileChoice = fourBoardTiles[chosenTileAndToken[0]];
tokenChoice = fourWildLifeTokens[chosenTileAndToken[1]];
choicePicked = true;
}
case 2 -> {
//b) Wipe out any number tokens and get a new selection
//ask which ones they want to wipe : a, b, c, or d
int[] choice = view.UseBotTokenOption2(tokensScores);
Controller.replaceNTokens(box.WildLifeTokens, fourWildLifeTokens, choice);
view.printBotBoards(bots, fourBoardTiles, fourWildLifeTokens);
}
}
bots.get(i).setNumNatureTokens(bots.get(i).getNumNatureTokens() - 1);
} else {
view.displayBotChoice(bots.get(i), "to not use a nature token.");
}
}
:: ‫إذا كان كود الوقت ينص على أن‬
‫ الحالية‬Turn‫عند إنتهاء ال‬
‫ ثواني‬5 ‫ التالية بفارق‬Turn ‫تتبعها ال‬
:: ‫قبل‬

: ‫بعد‬
if (i < 20) {
view.nextTurn();
//ask the user for input, next or quit
// Command c = view.getBotInput();
// if (c.isQuit()) {
// view.displayQuit();
// commandQuit = true;
// }
}
‫ التاليين هما كودين اختياريين حيث تم برمجة بديل لهما من قبل فريقكم وفريقنا ويجب تنفيذهما معا او عدم‬2 ‫ و‬1 ‫إختياري ( الكودين رقم‬
)‫تنفذيها معا‬

1 ‫كود رقم‬
: ‫قبل‬

::‫بعد‬
//Function to choose a tile and token pair and place it on the board
//returns 1,2,3,or 4 corresponding to pairs a,b,c, or d
//always chooses first pair for now
/// update-- now choosing the best pair
public static int chooseTokenTilePair(int[] tilesScores, int[] tokensScores) {
int maxIndex = 0;

for (int i = 1; i < tilesScores.length; i++) {


int score = tilesScores[i] + tokensScores[i];
if (score > tilesScores[maxIndex] + tokensScores[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}
:: 2 ‫كود‬
:: ‫قبل‬

::‫بعد‬
int choice = 4;
while (choice == 4) {
choice = chooseTokenTilePair(tilesScores, tokensScores);
String stringChoice = switch (choice) {
case 0 -> "a";
case 1 -> "b";
case 2 -> "c";
case 3 -> "d";
default -> " ";
};
view.displayBotChoice(bots.get(i), stringChoice);
}
chooseBotUseNatureToken ‫تعديل ميثود‬

‫ وهذه القيمة يمكن‬٢ ‫ تزيد عن‬tile ‫ و ال‬token ‫ الناتجة عن اختيار اي من ال‬score ‫هذا الكود يقوم بفحص اذا كانت ال‬
‫تغييرها‬
nature token ‫فإن كانت أعلى فإنه ال يستخدم‬
‫أما إن كانت أقل فانه يقوم باستخدامها ألجل الحصول على نتيجة افضل‬
:‫قبل‬

::‫بعد‬
public static boolean chooseBotUseNatureToken(int[] tilesScores, int[] tokensScores) {
try {

for (int i = 0; i < tilesScores.length; i++) {


int score = tilesScores[i] + tokensScores[i];
if (score > 2) return false;
}
return true;
} catch (Exception e) {
return false;
}

}
chooseBotNatureTokenUseType ‫تعديل ميثود‬

nature token ‫يقوم هذا الكود بنفس الكود السابق ولكن هذه المرة لتحديد اي طريقة الستخدام ال‬

‫ منفصلين والحصول على نتيجة جيدة فإنه يقوم بذلك‬tile ‫ و‬token ‫فإن كان من الممكن اختيار‬

wildlife token ‫وإال فإنه يقوم بإستبدال ال‬

::‫قبل‬

::‫بعد‬
public static int chooseBotNatureTokenUseType(int[] tilesScores, int[] tokensScores) {
// if a tile and a token has a good score choose one
// else choose two to change the tokens

for (int tilesScore : tilesScores) {


for (int tokensScore : tokensScores) {
int score = tilesScore + tokensScore;
if (score > 2) return 1;
}
}
return 2;
}
‫تعديل من‬
:: ‫قبل‬

‫بعد‬

public static String chooseATileFromFour(int[] tilesScores) {


return getBest(tilesScores);

}
public static String chooseATokenFromFour(int[] tokensScores) {
return getBest(tokensScores);

getBest ‫إضافة الميثود التالية‬

tile ‫ أو‬token ‫تقوم هذه الميثود بإختيار افضل‬

‫وحسب الميثودين السابقتين فإنها تقؤم بذلك بشكل منفصل‬


::‫جديد‬
private static String getBest(int[] tokensScores) {
char[] letters = {'a', 'b', 'c', 'd'};

int maxIndex = 0;

for (int i = 1; i < tokensScores.length; i++) {


if (tokensScores[i] > tokensScores[maxIndex]) {
maxIndex = i;
}
}

return String.valueOf(letters[maxIndex]);
}
‫إضافة الميثود‬

nature tokeen ‫ ليتم استبدالها بواسطة ال‬wildlife token ‫تقوم هذه الميثود بإختيار أسوأ‬

::‫جديد‬
public static String chooseBadTokenFromFour(int[] tokensScores) {
char[] letters = {'a', 'b', 'c', 'd'};

int minIndex = 0;

for (int i = 1; i < tokensScores.length; i++) {


if (tokensScores[i] < tokensScores[minIndex]) {
minIndex = i;
}
}
return String.valueOf(letters[minIndex]);

}
View.java ‫ملف‬
‫استبدال الميثودين التاليتين‬
‫ في ملف بوت‬8‫تم تعديل هاتين الميثود لكي تستخدم الميثود الجديدة المضافة‬
‫بدال من االختيار العشوائي‬
::‫قبل‬
public int[] UseBotTokenOption1() {

int[] options = new int[2];


do {
options[0] = -1;
System.out.println("Choose any board tile. (a/b/c/d)");
String str = Bot.chooseATileFromFour();
str = str.trim().toLowerCase();
switch (str) {
case "a" -> options[0] = 0;
case "b" -> options[0] = 1;
case "c" -> options[0] = 2;
case "d" -> options[0] = 3;
}
} while (options[0] == -1);
System.out.println("Chosen: " + options[0]);
do {
options[1] = -1;

System.out.println("Choose any wildlife token. (a/b/c/d)");

String str = Bot.chooseATileFromFour();


str = str.trim().toLowerCase();
switch (str) {
case "a" -> options[1] = 0;
case "b" -> options[1] = 1;
case "c" -> options[1] = 2;
case "d" -> options[1] = 3;
}
} while (options[1] == -1);
System.out.println("Chosen: " + options[1]);
return options;
}
::‫بعد‬
public int[] UseBotTokenOption1(int[] tilesScores, int[] tokensScores) {

int[] options = new int[2];

do {
options[0] = -1;
System.out.println("Choose any board tile. (a/b/c/d)");
String str = Bot.chooseATileFromFour(tilesScores);
str = str.trim().toLowerCase();
switch (str) {
case "a" -> options[0] = 0;
case "b" -> options[0] = 1;
case "c" -> options[0] = 2;
case "d" -> options[0] = 3;
}
} while (options[0] == -1);
System.out.println("Chosen: " + options[0]);

do {
options[1] = -1;

System.out.println("Choose any wildlife token. (a/b/c/d)");

String str = Bot.chooseATokenFromFour(tokensScores);


str = str.trim().toLowerCase();
switch (str) {
case "a" -> options[1] = 0;
case "b" -> options[1] = 1;
case "c" -> options[1] = 2;
case "d" -> options[1] = 3;
}
} while (options[1] == -1);
System.out.println("Chosen: " + options[1]);
return options;
}
::‫قبل‬

public int[] UseBotTokenOption2() {


//Wipe out any number tokens and get a new selection
//ask which ones they want to wipe : a, b, c, or d
System.out.println("How many tokens would you like to replace?");
int numReplacedTokens;
do {
System.out.println("Enter a number between 1 and 4.");
numReplacedTokens = Bot.chooseANumber1To4();
} while (numReplacedTokens < 1 || numReplacedTokens > 4);

int[] option = new int[numReplacedTokens];


System.out.println("Chosen: " + option.length);
for (int i = 0; i < numReplacedTokens; i++) {
String str;
do {

System.out.println("Choose wildlife token " + i + ". (a/b/c/d)");

str = Bot.chooseATileFromFour();
if (str.equalsIgnoreCase("a")) {
option[i] = 1;
} else if (str.equalsIgnoreCase("b")) {
option[i] = 2;
} else if (str.equalsIgnoreCase("c")) {
option[i] = 3;
} else if (str.equalsIgnoreCase("d")) {
option[i] = 4;
}
} while (option[i] != 1 && option[i] != 2 && option[i] != 3 && option[i] != 4);
System.out.println("Chosen: " + str);
}
return option;
}
::‫بعد‬

public int[] UseBotTokenOption2(int[] tilesScores) {


//Wipe out any number tokens and get a new selection
//ask which ones they want to wipe : a, b, c, or d
System.out.println("How many tokens would you like to replace?");
int numReplacedTokens;
do {
System.out.println("Enter a number between 1 and 4.");
numReplacedTokens = Bot.chooseANumber1To4();
} while (numReplacedTokens < 1 || numReplacedTokens > 4);

int[] option = new int[numReplacedTokens];


System.out.println("Chosen: " + option.length);
for (int i = 0; i < numReplacedTokens; i++) {
String str;
do {

System.out.println("Choose wildlife token " + i + ". (a/b/c/d)");

str = Bot.chooseBadTokenFromFour(tilesScores);
if (str.equalsIgnoreCase("a")) {
option[i] = 1;
} else if (str.equalsIgnoreCase("b")) {
option[i] = 2;
} else if (str.equalsIgnoreCase("c")) {
option[i] = 3;
} else if (str.equalsIgnoreCase("d")) {
option[i] = 4;
}
} while (option[i] != 1 && option[i] != 2 && option[i] != 3 && option[i] != 4);
System.out.println("Chosen: " + str);
}
return option;
}
::‫إستبدال الميثودين التاليتين بالجديدات‬

‫نفس الشرح السابق‬


::‫قبل‬

::‫بعد‬
public boolean askBotUseToken(int[] tilesScores, int[] tokensScores) {
System.out.println("Would you like to use a nature token? (y/n)");
boolean answer = Bot.chooseBotUseNatureToken(tilesScores,tokensScores);
System.out.println("Chosen answer: " + (answer ? "Y" : "N"));
return answer;
}

public int askBotTokenUseOptions(int[] tilesScores, int[] tokensScores) {


System.out.println("There are two options.");
System.out.println("Enter 1 to be able to choose any tile or token available. They do not
need to be a combination, or on the same column.");
System.out.println("Enter 2 to wipe any number of tokens from the market and refill
before choosing any combination.");
int answer = Bot.chooseBotNatureTokenUseType(tilesScores,tokensScores);
System.out.println("choosen answer: " + answer);
return answer;
}
‫إضافة الكود التالي‬
‫هذا هو الكود المسؤول عن الوقت حيث يتم استدعائه داخل ملف بوت‬

::‫جديد‬
public void nextTurn() {
int i = 4;
System.out.print("Next Turn in 5...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

do {
System.out.print(i + "...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
i--;
} while (i > 0);
System.out.println("Starting Next Turn...");

You might also like