import 'dart:math';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class Game extends StatefulWidget {
@override
_GameState createState() => _GameState();
}
class _GameState extends State<Game> {
var _imageAppChoice=AssetImage("images/base.png");
var _batleResult="Batle Field";
var _resultado="";
var _userVictory=0, _appVictory=0;
var _imageBatleResult=AssetImage("images/base.png");
void _selectOption(String userChoice) {
var options = ["rock", "paper", "scissors", "lizard", "spock"];
var number = Random().nextInt(5);
var appChoice = options[number];
//Show App Choice Image
switch (appChoice) {
case "rock":
setState(() {
this._imageAppChoice = AssetImage("images/rock.png");
});
break;
case "paper":
setState(() {
this._imageAppChoice = AssetImage("images/paper.png");
});
break;
case "scissors":
setState(() {
this._imageAppChoice = AssetImage("images/scissors.png");
});
break;
case "lizard":
setState(() {
this._imageAppChoice = AssetImage("images/lizard.png");
});
break;
case "spock":
setState(() {
this._imageAppChoice = AssetImage("images/spock.png");
});
break;
}
//If User Wins: Change Imagem
if(userChoice=="rock" && appChoice == "scissors") {
this._imageBatleResult=AssetImage("images/rock_scissors_win.png");
_batleResult="You Win! Your rock crushed the scissors";
}
if(userChoice=="rock" && appChoice == "lizard") {
this._imageBatleResult=AssetImage("images/rock_lizard_win.png");
_batleResult="You Win! Your rock smashed the lizard";
}
if(userChoice=="lizard" && appChoice == "paper") {
this._imageBatleResult=AssetImage("images/lizard_paper_win.png");
_batleResult="You Win! Your lizard ate the paper";
}
if(userChoice=="lizard" && appChoice == "spock") {
this._imageBatleResult=AssetImage("images/lizard_spock_win.png");
_batleResult="You Win! Your lizard poisoned the Spock";
}
if(userChoice=="spock" && appChoice == "scissors") {
this._imageBatleResult=AssetImage("images/spock_scissors_win.png");
_batleResult="You Win! Your Spock destroyed the scissors";
}
if(userChoice=="spock" && appChoice == "rock") {
this._imageBatleResult=AssetImage("images/spock_rock_win.png");
_batleResult="You Win! Your Spock vaporized the rock";
}
if(userChoice=="scissors" && appChoice == "paper") {
this._imageBatleResult=AssetImage("images/scissors_paper_win.png");
_batleResult="You Win! Your scissors cut the paper";
}
if(userChoice=="scissors" && appChoice == "lizard") {
this._imageBatleResult=AssetImage("images/scissors_lizard_win.png");
_batleResult="You Win! Your scissors beheaded the lizard";
}
if(userChoice=="paper" && appChoice == "rock") {
this._imageBatleResult=AssetImage("images/paper_rock_win.png");
_batleResult="You Win! Your paper covered the rock";
}
if(userChoice=="paper" && appChoice == "spock") {
this._imageBatleResult=AssetImage("images/paper_spock_win.png");
_batleResult="You Win! Your paper disapproves Spock";
}
//Image when user lose
if(appChoice=="rock" && userChoice == "scissors") {
this._imageBatleResult=AssetImage("images/rock_scissors_lose.png");
_batleResult="You Lose! Your scissors were destroyed by the rock";
}
if(appChoice=="rock" && userChoice == "lizard") {
this._imageBatleResult=AssetImage("images/rock_lizard_lose.png");
_batleResult="You Lose! Your lizard was smashed by the rock";
}
if(appChoice=="lizard" && userChoice == "paper") {
this._imageBatleResult=AssetImage("images/lizard_paper_lose.png");
_batleResult="You Lose! Your paper was eaten by the lizard";
}
if(appChoice=="lizard" && userChoice == "spock") {
this._imageBatleResult=AssetImage("images/lizard_spock_lose.png");
_batleResult="You Lose! Your Spock was poisoned by the lizard";
}
if(appChoice=="spock" && userChoice == "scissors") {
this._imageBatleResult=AssetImage("images/spock_scissors_lose.png");
_batleResult="You Lose! Your scissors were destroyed by Spock";
}
if(appChoice=="spock" && userChoice == "rock") {
this._imageBatleResult=AssetImage("images/spock_rock_lose.png");
_batleResult="You Lose! Your rock was vaporized by Spock";
}
if(appChoice=="scissors" && userChoice == "paper") {
this._imageBatleResult=AssetImage("images/scissors_paper_lose.png");
_batleResult="You Lose! Your paper was cut by scissors";
}
if(appChoice=="scissors" && userChoice == "lizard") {
this._imageBatleResult=AssetImage("images/scissors_lizard_lose.png");
_batleResult="You Lose! Your lizard was beheaded by scissors";
}
if(appChoice=="paper" && userChoice == "rock") {
this._imageBatleResult=AssetImage("images/paper_rock_lose.png");
_batleResult="You Lose! Your rock was covered by the paper";
}
if(appChoice=="paper" && userChoice == "spock") {
this._imageBatleResult=AssetImage("images/paper_spock_lose.png");
_batleResult="You Lose! Your Spock was disapproved by the paper";
}
//Check if user wins
if (
(userChoice == "rock" && appChoice == "scissors") ||
(userChoice == "rock" && appChoice == "lizard") ||
(userChoice == "lizard" && appChoice == "paper") ||
(userChoice == "lizard" && appChoice == "spock") ||
(userChoice == "spock" && appChoice == "scissors") ||
(userChoice == "spock" && appChoice == "rock") ||
(userChoice == "scissors" && appChoice == "paper") ||
(userChoice == "scissors" && appChoice == "lizard") ||
(userChoice == "paper" && appChoice == "rock") ||
(userChoice == "paper" && appChoice == "spock")
) {
setState(() {
_userVictory=_userVictory+1;
_resultado="win";
});
//Check if App wins
} else if (
(appChoice == "rock" && userChoice == "scissors") ||
(appChoice == "rock" && userChoice == "lizard") ||
(appChoice == "lizard" && userChoice == "paper") ||
(appChoice == "lizard" && userChoice == "spock") ||
(appChoice == "spock" && userChoice == "scissors") ||
(appChoice == "spock" && userChoice == "rock") ||
(appChoice == "scissors" && userChoice == "paper") ||
(appChoice == "scissors" && userChoice == "lizard") ||
(appChoice == "paper" && userChoice == "rock") ||
(appChoice == "paper" && userChoice == "spock")
) {
setState(() {
_appVictory=_appVictory+1;
_resultado="lose";
});
//Else - Draw!
} else {
setState(() {
_batleResult = "Draw! You chose the same as the App!";
this._imageBatleResult=AssetImage("images/draw.png");
_resultado="draw";
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("JanKenPon")
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 6, bottom: 4),
child: Text(
"App choice",
textAlign: TextAlign.right,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.bold
),
),
),
Image(image: this._imageAppChoice, height: 60,),
Padding(
padding: EdgeInsets.only(top: 6, bottom: 6, left: 6, right: 6),
child: Text(
this._batleResult,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 14,
fontWeight: _resultado=="win"? FontWeight.bold : FontWeight.normal ,
color: _resultado=="win"? Colors.green : _resultado=="lose"? Colors.deepOrange : Colors.black,
),
),
),
Image(image: this._imageBatleResult, height: 90,),
Padding(
padding: EdgeInsets.only(top: 6, bottom: 4),
child: Text(
"Choose your option",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.bold
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
GestureDetector(
onTap: ()=>_selectOption("rock"),
child: Image.asset("images/rock.png", height: 80,),
),
GestureDetector(
onTap: ()=>_selectOption("paper"),
child: Image.asset("images/paper.png", height: 70,),
),
GestureDetector(
onTap: ()=>_selectOption("scissors"),
child: Image.asset("images/scissors.png", height: 70,),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
GestureDetector(
onTap: ()=>_selectOption("lizard"),
child: Image.asset("images/lizard.png", height: 70,),
),
GestureDetector(
onTap: ()=>_selectOption("spock"),
child: Image.asset("images/spock.png", height: 70,),
),
],
),
Padding(
padding: EdgeInsets.only(top: 12, bottom: 0),
child: Text(
"You: $_userVictory | App: $_appVictory",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20,
fontWeight: _userVictory>_appVictory ? FontWeight.bold : FontWeight.normal ,
color: _userVictory>_appVictory ? Colors.green : _appVictory>_userVictory ? Colors.deepOrange : Colors.black,
),
),
),
]),
);
}
}