AIPlayer code example needed
Hello everyone,
I have an assignment for my AI Programming course which is due tomorrow, but I am stuck on a minor issue.
So this is a dice game where either the human player or an AI player has to roll 5 dice, and hope for on of the following: 2 pair, 3 of a kind, 4 of a kind, full house, small straight, large strait.
I've got the dice rolling and checking for combos of dice working.
Now it will select one of the 6 possible combos if there is a match, however it should also keep certain die for possible combos a possible after the second roll.
for example: after first roll I have 3 of a kind but 4 of a kind hasn't been selected/roll yet I want the AIPlayer to keep these 3 die to have a chance of getting 4 of a kind after the second roll.
So all code is there in the CheckDice method, but I have no clue how to loop trough this in the AIPlayer method to keep die or not.
If someone pls give me one or two examples with the provided code, that would be awesome.
this is my code for this game so far:
public class DiceGame : MonoBehaviour { #region MYCODE
enum gameState {
FirstRoll,
FirstWait,
FirstCheck,
SecondWait,
SecondRoll,
ThirdWait,
SecondCheck,
PlayerSwitch
};
public bool switchPlayer;
public float wait = 5f;
gameState AIGameState;
int player1AILevel;
int player2AILevel;
int IntervalToggle;
#endregion
int numTimesRolled; // This is the number of times that the player has rolled in a single turn.
int playerTurn; // This determines which player's turn it is.
int winner; // This holds the value of the player that has won.
bool diceRolling; // This bool is true when the dice are rolling.
bool diceChecked; // This bool is true if the dice have been check for combos.
int randomValue;
bool debugMode;
public Button submitButton;
#region Combo Identifiers
int onePair; // This stores the dieValue of the first pair found.
int twoPair; // This stores the dieValue of the second pair found.
int threeKind; // This stores the dieValue that appears at least 3 times.
int fourKind; // This stores the dieValue that appears at least 4 times.
int smallStraight; // This stores the largest dieValue included in the straight.
int largeStraight; // This stores the largest dieValue included in the straight.
int numInStraight;
#endregion
#region Combo lists
public PlayerCombo[] p1Combos; // This is an array of the combo information for player 1.
public PlayerCombo[] p2Combos; // This is an array of the combo information for player 2.
#endregion
#region Lists for the die values, totals and keep bools
int[] dieValue; // This is the current value of each of the dice.
public int[] diceTotals; // This is the current totals of the diceValues that were rolled.
bool[] keep; // This is an array of which dice are currently being kept.
#endregion
#region GUI Variables and Lists
public Image[] diceImages;
public Sprite[] diceSprites;
public Button[] keepButtons;
public Button rollButton;
public Text playerText;
#endregion
#region Variables to handle the rolling of the dice
float rollTime = 2.0f;
float rollStamp;
#endregion
// Initialization of variables
void Start ()
{
numTimesRolled = 0;
playerTurn = 1;
winner = 0;
playerText.text = "Player 1";
playerText.color = Color.green;
dieValue = new int[5];
diceTotals = new int[6];
keep = new bool[5] {false,false,false,false,false};
diceRolling = false;
diceChecked = true;
debugMode = false;
onePair = -1;
twoPair = -1;
threeKind = -1;
fourKind = -1;
smallStraight = -1;
largeStraight = -1;
#region MYCODE
player1AILevel = 0;
player2AILevel = 0;
#endregion
}
// Update is called once per frame
void Update ()
{
#region MYCODE
if (player1AILevel > 0)
{
AIPlayer();
}
if (player2AILevel > 0)
{
AIPlayer();
}
if (IntervalToggle == 0) {
wait = 5;
}
if (IntervalToggle == 1) {
wait = 6;
}
if (IntervalToggle == 2) {
wait = 7;
}
#endregion
// If debug mode in on allow the user to change the die values using the number keys from 1 - 5
if (debugMode)
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
dieValue[0]++;
if (dieValue[0] > 5)
{
dieValue[0] = 0;
}
diceImages[0].sprite = diceSprites[dieValue[0]];
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
dieValue[1]++;
if (dieValue[1] > 5)
{
dieValue[1] = 0;
}
diceImages[1].sprite = diceSprites[dieValue[1]];
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
dieValue[2]++;
if (dieValue[2] > 5)
{
dieValue[2] = 0;
}
diceImages[2].sprite = diceSprites[dieValue[2]];
}
if (Input.GetKeyDown(KeyCode.Alpha4))
{
dieValue[3]++;
if (dieValue[3] > 5)
{
dieValue[3] = 0;
}
diceImages[3].sprite = diceSprites[dieValue[3]];
}
if (Input.GetKeyDown(KeyCode.Alpha5))
{
dieValue[4]++;
if (dieValue[4] > 5)
{
dieValue[4] = 0;
}
diceImages[4].sprite = diceSprites[dieValue[4]];
}
}
if (diceRolling)
{
if (!debugMode)
{
// Pick a random number for each die.
for (int i = 0; i < 5; i++)
{
if (!keep[i])
{
randomValue = Random.Range(0, 6);
dieValue[i] = randomValue;
diceImages[i].sprite = diceSprites[randomValue];
}
}
// End the rolling after the rollTime is up.
if (Time.time > rollStamp + rollTime)
{
diceRolling = false;
rollButton.gameObject.SetActive(true);
numTimesRolled++;
if (numTimesRolled == 2)
{
rollButton.GetComponentInChildren<Text>().text = "Done";
}
}
}
}
else
{
// If the dice have been rolled once and the keep buttons are currently inactive.
if (numTimesRolled == 1 && keepButtons[0].gameObject.activeSelf == false)
{
// Activate all keep buttons and set to Roll.
for (int i = 0; i < 5; i++)
{
keepButtons[i].gameObject.SetActive(true);
keepButtons[i].GetComponentInChildren<Text>().text = "Roll";
}
}
// Check the dice after each roll.
if (!diceChecked)
{
CheckDice();
}
}
}
#region MYCODE
public void SetPlayer1AILevel(int level)
{
player1AILevel = level;
}
public void SetPlayer2AILevel(int level)
{
player2AILevel = level;
}
public void SetIntervalTime(int level)
{
IntervalToggle = level;
}
public void AIPlayer() {
switch (AIGameState) {
case gameState.FirstRoll:
RollDice ();
AIGameState = gameState.FirstWait;
break;
case gameState.FirstWait:
if (Time.time > rollStamp + wait) {
AIGameState = gameState.FirstCheck;
}
break;
case gameState.FirstCheck:
CheckDice ();
if (playerTurn == 1) {
if (largeStraight > -1) {
if (!p1Combos [5].GetSelected ()) {
p1Combos [5].SetSelected (true);
switchPlayer = true;
}
} else if (smallStraight > -1) {
if (!p1Combos [4].GetSelected ()) {
p1Combos [4].SetSelected (true);
switchPlayer = true;
}
} else if (onePair > -1 && threeKind > -1) {
if (!p1Combos [3].GetSelected ()) {
p1Combos [3].SetSelected (true);
switchPlayer = true;
}
} else if (fourKind > -1) {
if (!p1Combos [2].GetSelected ()) {
p1Combos [2].SetSelected (true);
switchPlayer = true;
}
} else if (threeKind > -1) {
if (!p1Combos [1].GetSelected ()) {
p1Combos [1].SetSelected (true);
switchPlayer = true;
}
} else if (twoPair > -1) {
if (!p1Combos [0].GetSelected ()) {
p1Combos [0].SetSelected (true);
switchPlayer = true;
}
}
} else {
if (largeStraight > -1) {
if (!p2Combos [5].GetSelected ()) {
p2Combos [5].SetSelected (true);
switchPlayer = true;
}
} else if (smallStraight > -1) {
if (!p2Combos [4].GetSelected ()) {
p2Combos [4].SetSelected (true);
switchPlayer = true;
}
} else if (onePair > -1 && threeKind > -1) {
if (!p2Combos [3].GetSelected ()) {
p2Combos [3].SetSelected (true);
switchPlayer = true;
}
} else if (fourKind > -1) {
if (!p2Combos [2].GetSelected ()) {
p2Combos [2].SetSelected (true);
switchPlayer = true;
}
} else if (threeKind > -1) {
if (!p2Combos [1].GetSelected ()) {
p2Combos [1].SetSelected (true);
switchPlayer = true;
}
} else if (twoPair > -1) {
if (!p2Combos [0].GetSelected ()) {
p2Combos [0].SetSelected (true);
switchPlayer = true;
}
}
}
if (switchPlayer == true) {
numTimesRolled++;
switchPlayer = false;
AIGameState = gameState.PlayerSwitch;
} else {
AIGameState = gameState.SecondWait;
}
break;
case gameState.SecondWait:
AIGameState = gameState.SecondRoll;
break;
case gameState.SecondRoll:
RollDice ();
AIGameState = gameState.ThirdWait;
break;
case gameState.ThirdWait:
if (Time.time > rollStamp + wait) {
AIGameState = gameState.SecondCheck;
}
break;
case gameState.SecondCheck:
CheckDice ();
if (playerTurn == 1) {
if (largeStraight > -1) {
if (!p1Combos [5].GetSelected ()) {
p1Combos [5].SetSelected (true);
// switchPlayer = true;
}
} else if (smallStraight > -1) {
if (!p1Combos [4].GetSelected ()) {
p1Combos [4].SetSelected (true);
// switchPlayer = true;
}
} else if (onePair > -1 && threeKind > -1) {
if (!p1Combos [3].GetSelected ()) {
p1Combos [3].SetSelected (true);
// switchPlayer = true;
}
} else if (fourKind > -1) {
if (!p1Combos [2].GetSelected ()) {
p1Combos [2].SetSelected (true);
// switchPlayer = true;
}
} else if (threeKind > -1) {
if (!p1Combos [1].GetSelected ()) {
p1Combos [1].SetSelected (true);
// switchPlayer = true;
}
} else if (twoPair > -1) {
if (!p1Combos [0].GetSelected ()) {
p1Combos [0].SetSelected (true);
// switchPlayer = true;
}
}
} else {
if (largeStraight > -1) {
if (!p2Combos [5].GetSelected ()) {
p2Combos [5].SetSelected (true);
// switchPlayer = true;
}
} else if (smallStraight > -1) {
if (!p2Combos [4].GetSelected ()) {
p2Combos [4].SetSelected (true);
// switchPlayer = true;
}
} else if (onePair > -1 && threeKind > -1) {
if (!p2Combos [3].GetSelected ()) {
p2Combos [3].SetSelected (true);
// switchPlayer = true;
}
} else if (fourKind > -1) {
if (!p2Combos [2].GetSelected ()) {
p2Combos [2].SetSelected (true);
// switchPlayer = true;
}
} else if (threeKind > -1) {
if (!p2Combos [1].GetSelected ()) {
p2Combos [1].SetSelected (true);
// switchPlayer = true;
}
} else if (twoPair > -1) {
if (!p2Combos [0].GetSelected ()) {
p2Combos [0].SetSelected (true);
// switchPlayer = true;
}
}
}
if (Time.time > rollStamp + wait) {
AIGameState = gameState.PlayerSwitch;
}
break;
case gameState.PlayerSwitch:
if (!diceRolling && numTimesRolled == 2) {
ResetComboButtons ();
if (playerTurn == 1) {
playerTurn = 2;
playerText.text = "Player 2";
playerText.color = Color.red;
} else {
playerTurn = 1;
playerText.text = "Player 1";
playerText.color = Color.green;
}
numTimesRolled = 0;
rollButton.GetComponentInChildren<Text> ().text = "Roll Dice";
// Reset all keep variables.
for (int i = 0; i < 5; i++) {
keep [i] = false;
}
}
if (playerTurn == 2) {
if (Time.time > rollStamp + wait) {
if (player2AILevel > 0) {
AIGameState = gameState.FirstRoll;
}
}
}
if (playerTurn == 1) {
if (Time.time > rollStamp + wait) {
if (player1AILevel > 0) {
AIGameState = gameState.FirstRoll;
}
}
}
break;
default:
Debug.Log("error, no gameState available!!!");
break;
}
}
#endregion
public void RollDice()
{
#region Reset combo identifiers and buttons every time the dice are rolled.
onePair = -1;
twoPair = -1;
threeKind = -1;
fourKind = -1;
smallStraight = -1;
largeStraight = -1;
ResetComboButtons();
#endregion
// If someone wins reset all variables and start a new game.
if (winner > 0)
{
numTimesRolled = 0;
playerTurn = 1;
winner = 0;
playerText.text = "Player 1";
playerText.color = Color.green;
dieValue = new int[5];
diceTotals = new int[6];
keep = new bool[5] {false,false,false,false,false};
diceRolling = false;
diceChecked = true;
for (int i = 0; i < p1Combos.Length; i++)
{
p1Combos[i].SetSelected(false);
p2Combos[i].SetSelected(false);
}
ResetComboButtons();
rollButton.GetComponentInChildren<Text>().text = "Roll Dice";
}
else
{
// If the dice are not yet rolling and the player has rolled the dice less than two times.
if (!diceRolling && numTimesRolled < 2)
{
diceRolling = true;
rollStamp = Time.time;
diceChecked = false;
// Deactivate all keep and roll buttons.
rollButton.gameObject.SetActive(false);
for (int i = 0; i < 5; i++)
{
keepButtons[i].gameObject.SetActive(false);
}
}
// If the dice are done rolling and the player has rolled two times then the turn is over.
if (!diceRolling && numTimesRolled == 2)
{
ResetComboButtons();
if (playerTurn == 1)
{
playerTurn = 2;
playerText.text = "Player 2";
playerText.color = Color.red;
}
else
{
playerTurn = 1;
playerText.text = "Player 1";
playerText.color = Color.green;
}
numTimesRolled = 0;
rollButton.GetComponentInChildren<Text>().text = "Roll Dice";
// Reset all keep variables.
for (int i = 0; i < 5; i++)
{
keep[i] = false;
}
}
}
}
public void debugToggle()
{
// Toggle debug mode.
if (debugMode)
{
submitButton.gameObject.SetActive(false);
}
else
{
submitButton.gameObject.SetActive(true);
}
debugMode = !debugMode;
}
public void submitChanges()
{
if (diceRolling)
{
diceRolling = false;
rollButton.gameObject.SetActive(true);
numTimesRolled++;
if (numTimesRolled == 2)
{
rollButton.GetComponentInChildren<Text>().text = "Done";
}
}
}
public void keepToggle(int index)
{
// Toggle keep buttons when pressed.
if (keep[index])
{
keep[index] = false;
keepButtons[index].GetComponentInChildren<Text>().text = "Roll";
}
else
{
keep[index] = true;
keepButtons[index].GetComponentInChildren<Text>().text = "Keep";
}
}
void CheckDice()
{
#region Reset straight check and dicTotals.
numInStraight = 0;
for (int i = 0; i < 6; i++)
{
diceTotals[i] = 0;
}
#endregion
#region Add diceValues to the diceTotals.
for (int i = 0; i < 5; i++)
{
diceTotals[dieValue[i]]++;
}
#endregion
for (int i = 0; i < 6; i++)
{
// For each diceTotal that is more than 1 add to the straight check until a diceTotal of zero is found.
// That will tell you how if you have four or five numbers in a row.
if (diceTotals[i] > 0)
{
numInStraight++;
if (numInStraight == 4)
{
smallStraight = i;
}
else if (numInStraight == 5)
{
largeStraight = i;
}
}
else
{
numInStraight = 0;
}
// If a diceTotal is equal to two and you have not yet identified a pair.
if (diceTotals[i] == 2 && onePair == -1)
{
onePair = i;
}
// If a diceTotal is equal to two and you have identified a pair.
// Check to make sure the pairs are not the same diceValue.
if (diceTotals[i] == 2 && onePair > -1 && onePair != i)
{
twoPair = i;
}
// If a diceTotal is greater than two you have three of a kind.
if (diceTotals[i] > 2)
{
threeKind = i;
}
// If a diceTotal is greater than three you have four of a kind.
if (diceTotals[i] > 3)
{
fourKind = i;
}
}
// If a pair has been found along with either a second pair or three of a kind.
// Then allow selection of the two pair button.
if (twoPair > -1 || (onePair > -1 && threeKind > -1))
{
if (playerTurn == 1)
{
if (!p1Combos[0].GetSelected())
{
p1Combos[0].buttonReference.gameObject.SetActive(true);
p1Combos[0].buttonReference.GetComponentInChildren<Text>().text = "Select";
}
}
else
{
if (!p2Combos[0].GetSelected())
{
p2Combos[0].buttonReference.gameObject.SetActive(true);
p2Combos[0].buttonReference.GetComponentInChildren<Text>().text = "Select";
}
}
}
// If a three of a kind has been found then allow selection of the three of a kind button.
if (threeKind > -1)
{
if (playerTurn == 1)
{
if (!p1Combos[1].GetSelected())
{
p1Combos[1].buttonReference.gameObject.SetActive(true);
p1Combos[1].buttonReference.GetComponentInChildren<Text>().text = "Select";
}
}
else
{
if (!p2Combos[1].GetSelected())
{
p2Combos[1].buttonReference.gameObject.SetActive(true);
p2Combos[1].buttonReference.GetComponentInChildren<Text>().text = "Select";
}
}
}
// If a four of a kind has been found then allow selection of the four of a kind button.
if (fourKind > -1)
{
if (playerTurn == 1)
{
if (!p1Combos[2].GetSelected())
{
p1Combos[2].buttonReference.gameObject.SetActive(true);
p1Combos[2].buttonReference.GetComponentInChildren<Text>().text = "Select";
}
}
else
{
if (!p2Combos[2].GetSelected())
{
p2Combos[2].buttonReference.gameObject.SetActive(true);
p2Combos[2].buttonReference.GetComponentInChildren<Text>().text = "Select";
}
}
}
// If a three of a kind and a pair has been found then allow selection of the full house button.
if (threeKind > -1 && onePair > -1)
{
if (playerTurn == 1)
{
if (!p1Combos[3].GetSelected())
{
p1Combos[3].buttonReference.gameObject.SetActive(true);
p1Combos[3].buttonReference.GetComponentInChildren<Text>().text = "Select";
}
}
else
{
if (!p2Combos[3].GetSelected())
{
p2Combos[3].buttonReference.gameObject.SetActive(true);
p2Combos[3].buttonReference.GetComponentInChildren<Text>().text = "Select";
}
}
}
// If a small straight has been found then allow selection of the small straight button.
if (smallStraight > -1)
{
if (playerTurn == 1)
{
if (!p1Combos[4].GetSelected())
{
p1Combos[4].buttonReference.gameObject.SetActive(true);
p1Combos[4].buttonReference.GetComponentInChildren<Text>().text = "Select";
}
}
else
{
if (!p2Combos[4].GetSelected())
{
p2Combos[4].buttonReference.gameObject.SetActive(true);
p2Combos[4].buttonReference.GetComponentInChildren<Text>().text = "Select";
}
}
}
// If a large straight has been found then allow selection of the large straight button.
if (largeStraight > -1)
{
if (playerTurn == 1)
{
if (!p1Combos[5].GetSelected())
{
p1Combos[5].buttonReference.gameObject.SetActive(true);
p1Combos[5].buttonReference.GetComponentInChildren<Text>().text = "Select";
}
}
else
{
if (!p2Combos[5].GetSelected())
{
p2Combos[5].buttonReference.gameObject.SetActive(true);
p2Combos[5].buttonReference.GetComponentInChildren<Text>().text = "Select";
}
}
}
diceChecked = true; // Identify that the dice have been checked for combos this roll.
}
public void SelectCombo(int index)
{
// Check to see which player selected the combo.
if (playerTurn == 1)
{
p1Combos[index].SetSelected(true);
}
else
{
p2Combos[index].SetSelected(true);
}
// End the current player's turn.
numTimesRolled = 2;
rollButton.GetComponentInChildren<Text>().text = "Done";
ResetComboButtons();
// Deactivate all keep buttons.
for (int i = 0; i < 5; i++)
{
keepButtons[i].gameObject.SetActive(false);
}
// Check to see if a player has won.
CheckForWin();
if (winner > 0)
{
rollButton.GetComponentInChildren<Text>().text = "Play Again";
}
}
void ResetComboButtons()
{
// Hide all combo buttons that have not been selected and set all the rest to say done and not be interactable.
for (int i = 0; i < p1Combos.Length; i++)
{
if (!p1Combos[i].GetSelected())
{
p1Combos[i].buttonReference.gameObject.SetActive(false);
p1Combos[i].buttonReference.interactable = true;
}
else
{
p1Combos[i].buttonReference.GetComponentInChildren<Text>().text = "Done";
p1Combos[i].buttonReference.interactable = false;
}
}
for (int i = 0; i < p2Combos.Length; i++)
{
if (!p2Combos[i].GetSelected())
{
p2Combos[i].buttonReference.gameObject.SetActive(false);
p2Combos[i].buttonReference.interactable = true;
}
else
{
p2Combos[i].buttonReference.GetComponentInChildren<Text>().text = "Done";
p2Combos[i].buttonReference.interactable = false;
}
}
}
void CheckForWin()
{
bool possibleWin1 = true;
bool possibleWin2 = true;
// If any player has a combo not selected then that player has not won.
for (int i = 0; i < p1Combos.Length; i++)
{
if (!p1Combos[i].GetSelected())
{
possibleWin1 = false;
}
}
for (int i = 0; i < p2Combos.Length; i++)
{
if (!p2Combos[i].GetSelected())
{
possibleWin2 = false;
}
}
if (possibleWin1)
{
winner = 1;
playerText.GetComponentInChildren<Text>().text = "Player 1 Wins!";
playerText.color = Color.blue;
}
else if (possibleWin2)
{
winner = 2;
playerText.GetComponentInChildren<Text>().text = "Player 2 Wins!";
playerText.color = Color.blue;
}
}
}
public class PlayerCombo { public bool selected = false; public Button buttonReference;
public bool GetSelected()
{
return selected;
}
public void SetSelected(bool value)
{
selected = value;
}
}
Is the question you are asking "How do I modify this so that my AI Player correctly updates the keep array in anticipation of certain potential rolls?"