Skip to content

Instantly share code, notes, and snippets.

@Yuhtin
Created May 31, 2021 01:40
Show Gist options
  • Save Yuhtin/fdfb70953b06d4886cbd7b3453344e48 to your computer and use it in GitHub Desktop.
Save Yuhtin/fdfb70953b06d4886cbd7b3453344e48 to your computer and use it in GitHub Desktop.
Jokenpo system
import 'package:flutter/material.dart';
import 'jokenpo.dart';
import 'jokenpoTypes.dart';
class Homepage extends StatefulWidget {
Homepage({Key key, title: 'Home'}) : super(key: key);
@override
_HomepageState createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
var _appOption = '-';
var _message = '';
var _totalWins = 0;
var _winsInARoll = 0;
void play(JokenpoTypes option) {
var result = Jokenpo.play(option);
var appOption = result['appOption'];
var optionAsMessage = 'Tesoura';
switch (appOption) {
case JokenpoTypes.STONE:
optionAsMessage = 'Pedra';
break;
case JokenpoTypes.PAPER:
optionAsMessage = 'Papel';
break;
default:
break;
}
var resultMessage = result['message'];
setState(() {
_appOption = optionAsMessage;
_message = resultMessage;
if (resultMessage.toString().contains("ganhou")) {
++_totalWins;
++_winsInARoll;
// show custom message
if (_winsInARoll >= 2) {
_message = _message + '\nJá é a $_winsInARollª vitória seguida';
}
if (_winsInARoll >= 4) {
_message =
_message + '\nVitória desenfreada!!!! Aposta na megasena logo!';
}
} else {
_winsInARoll = 0;
}
});
}
AppBar appBarBuilder() {
return AppBar(
title: Text('Vitórias $_totalWins'),
);
}
Widget jokenpo() {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 16, bottom: 16),
child: Text(
'Escolha do aplicativo',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
)),
Text(_appOption),
Expanded(
child: Center(
child: Text(
_message,
style: TextStyle(fontSize: 15, fontWeight: FontWeight.w700),
),
)),
Text(
'Escolha do jogador',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
Padding(
padding: EdgeInsets.only(top: 16, bottom: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
GestureDetector(
onTap: () => play(JokenpoTypes.STONE),
child: Text('Pedra'),
),
GestureDetector(
onTap: () => play(JokenpoTypes.PAPER),
child: Text('Papel'),
),
GestureDetector(
onTap: () => play(JokenpoTypes.SCISSORS),
child: Text('Tesoura'),
),
],
),
)
],
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: appBarBuilder(),
body: jokenpo(),
);
}
}
import 'dart:math';
import 'package:eai_flutter_class/jokenpoTypes.dart';
import 'jokenpoTypes.dart';
class Jokenpo {
static String calc(JokenpoTypes option, JokenpoTypes appOption) {
if ((option == JokenpoTypes.STONE && appOption == JokenpoTypes.SCISSORS) ||
(option == JokenpoTypes.SCISSORS && appOption == JokenpoTypes.PAPER) ||
(option == JokenpoTypes.PAPER && appOption == JokenpoTypes.STONE)) {
return 'Parabéns, você ganhou!';
}
if ((appOption == JokenpoTypes.STONE && option == JokenpoTypes.SCISSORS) ||
(appOption == JokenpoTypes.SCISSORS && option == JokenpoTypes.PAPER) ||
(appOption == JokenpoTypes.PAPER && option == JokenpoTypes.STONE)) {
return 'Você perdeu, tente novamente!';
}
return 'Empate';
}
static Map<String, dynamic> play(JokenpoTypes option) {
var result = new Map<String, dynamic>();
var appOption =
JokenpoTypes.values[Random().nextInt(JokenpoTypes.values.length)];
result['appOption'] = appOption;
result['message'] = calc(option, appOption);
return result;
}
}
enum JokenpoTypes { STONE, PAPER, SCISSORS }
import 'package:flutter/material.dart';
import 'home.dart';
const appName = "Aula Flutter";
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: appName,
darkTheme: ThemeData.dark().copyWith(primaryColor: Colors.green),
theme: ThemeData(
primarySwatch: Colors.red,
),
home: Homepage(),
debugShowCheckedModeBanner: false,
);
}
}
@Yuhtin
Copy link
Author

Yuhtin commented May 31, 2021

image
image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment