Skip to content

Commit

Permalink
AI implimented #34
Browse files Browse the repository at this point in the history
This is a temporary AI that will pull from the deck then throw a random card back
  • Loading branch information
FaisalAlwazir authored and FaisalAlwazir committed Aug 3, 2020
1 parent a3eef33 commit f374fc7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/models/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ class Player {
PlayingCard extraCard;
PositionOnScreen position;
PlayerInfo personalInfo = new PlayerInfo();
Player(this.position);
Player(this.position, {this.isAI = false});
bool discarded = true;
bool eligibleToDraw = true;
bool isAI = false;

void recordGame(PositionOnScreen winnerPosition) {
personalInfo.avgScore =
Expand Down
28 changes: 25 additions & 3 deletions lib/pages/game_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class GameScreen extends StatefulWidget {
class _GameScreenState extends State<GameScreen> {
// Stores the cards on the seven columns
List<Player> playersList = [
new Player(PositionOnScreen.left),
new Player(PositionOnScreen.top),
new Player(PositionOnScreen.right),
new Player(PositionOnScreen.left, isAI: true),
new Player(PositionOnScreen.top, isAI: true),
new Player(PositionOnScreen.right, isAI: true),
new Player(PositionOnScreen.bottom)
];
Player currentTurn;
Expand Down Expand Up @@ -313,6 +313,28 @@ class _GameScreenState extends State<GameScreen> {
.removeAt(_getListFromIndex(index).indexOf(cards.first));
_refreshList(index);
currentTurn = _getNextPlayer(currentTurn);

while (currentTurn.isAI) {
currentTurn.cards.shuffle();

/// in the commented line below, I tried to add some time before
/// the AI makes a decision but it needs to have an asynchronous
/// environment. This needs a lot of refactoring to the code.
//await new Future.delayed(const Duration(seconds: 5));

/// We can use this code however this freezes everything for the
/// set period of time, making the screen seem laggy or glitched.
//sleep(const Duration(seconds:1));

currentTurn.cards.add(cardDeckClosed.removeLast()
..faceUp = true
..opened = true);
droppedCards.add(currentTurn.cards[1]);
currentTurn.cards.removeAt(1);
currentTurn = _getNextPlayer(currentTurn);
currentTurn.initializeForNextTurn();
}

currentTurn.initializeForNextTurn();
}
},
Expand Down

0 comments on commit f374fc7

Please sign in to comment.