Flutter
[Flutter] 플러터 - 가위바위보 만들기
송 이
2024. 7. 8. 19:16
import 'dart:convert';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int total = 0;
int winCnt = 0;
int drawCnt = 0;
int loseCnt = 0;
String imgPath_com = "assets/images/scissor.png";
String imgPath_user = "assets/images/scissor.png";
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Builder(
builder: (context) {
return Scaffold(
appBar: AppBar(
title: Text("가위-바위-보"),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(
imgPath_user,
width: 100,
height: 100,
),
SizedBox(
width: 10,
),
Text(
'VS',
style:
TextStyle(fontSize: 36, fontWeight: FontWeight.bold),
),
SizedBox(
width: 10,
),
Image.asset(
imgPath_com,
width: 100,
height: 100,
),
],
),
SizedBox(
height: 50,
),
Container(
width: 400,
height: 200,
alignment: Alignment.center,
child: Text(
'${total}전 ${winCnt}승 ${drawCnt}무 ${loseCnt}패',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 40,
color: Colors.blueGrey,
),
),
),
SizedBox(
height: 50,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
_playGame(context, 1);
},
child: Text('가위'),
),
ElevatedButton(
onPressed: () {
_playGame(context, 2);
},
child: Text('바위'),
),
ElevatedButton(
onPressed: () {
_playGame(context, 3);
},
child: Text('보'),
),
],
)
],
),
);
},
),
);
}
void _playGame(BuildContext context, int userChoice) {
String result = '';
int comChoice = Random().nextInt(3) + 1;
print(comChoice);
String img = '';
if (comChoice == 1) {
img = 'scissor';
} else if (comChoice == 2) {
img = 'rock';
} else {
img = 'paper';
}
String userImg = '';
if (userChoice == 1) {
userImg = 'scissor';
} else if (userChoice == 2) {
userImg = 'rock';
} else {
userImg = 'paper';
}
setState(() {
imgPath_com = 'assets/images/${img}.png';
imgPath_user = 'assets/images/${userImg}.png';
total++;
result = _decision(comChoice, userChoice);
});
_showAlertDialog(context, result);
_sendData();
}
String _decision(int com, int user) {
int result = user - com;
String decision = '';
if (result == 1 || result == -2) {
decision = 'Win';
winCnt++;
} else if (result == 0) {
decision = 'Draw';
drawCnt++;
} else {
decision = 'Lose';
loseCnt++;
}
return decision;
}
void _showAlertDialog(BuildContext context, String result) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('승부는 ?'),
content: Text('You ${result}'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('close'),
)
],
);
},
);
}
Future<void> _sendData() async {
String url = 'http://192.168.0.233:9003/api/post';
final data = {
'total': total,
'win': winCnt,
'draw': drawCnt,
'lose': loseCnt
};
//post 요청 보내기
final response = await http.post(
Uri.parse(url),
body: jsonEncode(data),
headers: {'Content-Type': 'application/json'},
);
//응답 처리
if (response.statusCode == 200) {
print('Ok.... good : ' + response.body);
} else {
print('데이터 전송 실패 : ${response.statusCode}');
}
}
}