Skip to content

Commit

Permalink
Merge pull request #3 from alikhabazian/Players_class
Browse files Browse the repository at this point in the history
Players class
  • Loading branch information
alikhabazian authored Jan 15, 2024
2 parents d148f8d + 602c466 commit 2775339
Show file tree
Hide file tree
Showing 8 changed files with 426 additions and 126 deletions.
98 changes: 79 additions & 19 deletions secret_hitler/lib/GameSetting.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:flutter/material.dart';
import 'package:secret_hitler/rollassigning.dart';
import 'package:secret_hitler/state_management.dart';
import 'package:provider/provider.dart';
class GameSetting extends StatefulWidget {
const GameSetting({super.key, required this.title});

Expand All @@ -22,7 +24,11 @@ class _GameSetting extends State<GameSetting> {
int _numPlayers = 5;
List<TextEditingController> playerNameController = [];
List<Widget> playerWidgets = [];
List<Widget> expantionSetting = [];
bool first_time=true;
bool isCheckedLibPlus=false;
bool isCheckedFascistPlus=false;


void _incrementPlayers() {
setState(() {
Expand Down Expand Up @@ -62,36 +68,50 @@ class _GameSetting extends State<GameSetting> {
);
}

String gameType(){
if(isCheckedLibPlus){
return 'LibPlus';
}
else if(isCheckedFascistPlus){
return 'FascistPlus';
}
else{
return 'Normal';
}
}

void _setPlayers() {
List<String> playersName = [];
setState(() {
for (int i = 0; i < _numPlayers; i++) {
if (playerNameController[i].text.length != 0) {
playersName.add(
playerNameController[i].text
);
} else {
showMessageDialog(context, 'You must fill player ${i+1} name');
return;
// break;
}

for (int i = 0; i < _numPlayers; i++) {
if (playerNameController[i].text.length != 0) {
playersName.add(
playerNameController[i].text
);
} else {
showMessageDialog(context, 'You must fill player ${i+1} name');
return;
// break;
}
Navigator.push(
context,
MaterialPageRoute(builder: (context) => RollAssigning(data: playersName)),
);

}
GameState gameState = Provider.of<GameState>(context,listen: false);

gameState.setUp(playersName,gameType:gameType());
setState(() {
});
Navigator.push(
context,
MaterialPageRoute(builder: (context) => RollAssigning()),
);
}

void _setUp() {}


@override
Widget build(BuildContext context) {
// playerNameController = [];

playerWidgets = [];

if (first_time){
for (int i = 0; i < _numPlayers; i++) {
playerNameController.add(new TextEditingController());
Expand Down Expand Up @@ -147,6 +167,45 @@ class _GameSetting extends State<GameSetting> {
);
playerWidgets.add(SizedBox(height: 16.0));
}

expantionSetting = [];
if (_numPlayers==7||_numPlayers==9){//Liberal plus
Widget libPlusWidget=Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Radio(
value:true,
groupValue: isCheckedLibPlus,
onChanged: (bool? value) {
setState(() {
isCheckedLibPlus = value ?? false;
});
},
),
Text('Liberal plus'),
],
);
expantionSetting.add(libPlusWidget);
}
if (_numPlayers==8||_numPlayers==10){//Fascist Plus
Widget fascistPlusWidget=Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Radio(
value:true,
groupValue: isCheckedFascistPlus,
onChanged: (bool? value) {
setState(() {
isCheckedFascistPlus = value ?? false;

});
},
),
Text('Fascist plus'),
],
);
expantionSetting.add(fascistPlusWidget);
}
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
Expand Down Expand Up @@ -227,7 +286,8 @@ class _GameSetting extends State<GameSetting> {

],
),
]+playerWidgets,

]+expantionSetting+playerWidgets,
),
),),
floatingActionButton: FloatingActionButton(
Expand Down
15 changes: 5 additions & 10 deletions secret_hitler/lib/gameFunctions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ void showRoleDialog(BuildContext context,GameState game_state) {
context: context,
barrierDismissible:false,
builder: (BuildContext context) {
String rool='';
if (game_state.roles[game_state.names.indexOf(game_state.willSearch)]=='Liberal'){
rool='Liberal';
}
else{
rool='Fascist';
}
String role=game_state.findPlayerWithName(game_state.willSearch).searchResult();

return AlertDialog(
title: const Text('Message'),
// content: Text("You investigated ${name}, and their role is ${role}."),
Expand All @@ -23,7 +18,7 @@ void showRoleDialog(BuildContext context,GameState game_state) {
const TextSpan(text: 'You investigated '),
TextSpan(text: game_state.willSearch, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 18)),
const TextSpan(text: ' and their role is '),
TextSpan(text: '$rool.', style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 18)),
TextSpan(text: '$role.', style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 18)),

],
),
Expand All @@ -44,12 +39,12 @@ void showRoleDialog(BuildContext context,GameState game_state) {

void onPressedSuccessElection(BuildContext context,String name,GameState game_state) {
// first_selected=true;
if(game_state.veto && game_state.roles[game_state.names.indexOf(name)]=='Liberal' && game_state.roles[game_state.turn]=='Liberal'){
if(game_state.veto && game_state.findPlayerWithName(name).role=='Liberal' && game_state.players[game_state.turn].role=='Liberal'){
game_state.activateVeto();
}
game_state.changeState('elected');
game_state.changePresidentSelected(-1);
if(game_state.roles[game_state.names.indexOf(name)]=='Hitler'&& game_state.hitlerState){
if(game_state.findPlayerWithName(name).role=='Hitler'&& game_state.hitlerState){
game_state.hitlerChancellor();
}

Expand Down
10 changes: 4 additions & 6 deletions secret_hitler/lib/gameUI.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ import 'package:shared_preferences/shared_preferences.dart';


class Game extends StatefulWidget {
final List<String> name;
final List<String> roles;

const Game({Key? key, required this.name, required this.roles}) : super(key: key);
const Game({Key? key}) : super(key: key);

@override
_Game createState() => _Game();
Expand All @@ -32,9 +30,9 @@ class _Game extends State<Game> {
print("initState");
gameState = Provider.of<GameState>(context,listen: false);
// game_state = Game_state(names: widget.name, roles: widget.roles);
if(!gameState.continuegame) {
gameState.setUp(widget.name, widget.roles);
}
// if(!gameState.continuegame) {
// gameState.setUp(widget.name);
// }
print("initState after setUp");
super.initState();
_prefs.then((SharedPreferences prefs) {
Expand Down
17 changes: 9 additions & 8 deletions secret_hitler/lib/gameUIFunctions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,11 @@ void discardChancellor(GameState game_state){
}

void discardPresident(GameState game_state){
game_state.changeLastPresident(game_state.names[game_state.turn]);
// game_state.changeLastPresident(game_state.names[game_state.turn]);
game_state.changeLastPresident(game_state.players[game_state.turn].name);
game_state.changeState('president_discarded');
game_state.changeChancellorSelect(-1);
game_state.addLog("${game_state.names[game_state.turn]} discard:${game_state.dec[game_state.presidentSelected]}\n");
game_state.addLog("${game_state.players[game_state.turn].name} discard:${game_state.dec[game_state.presidentSelected]}\n");
game_state.disDec.add(game_state.dec.removeAt(game_state.presidentSelected));
game_state.updatePage();
}
Expand Down Expand Up @@ -194,7 +195,7 @@ List <Widget> getUiState(context,width,height,GameState game_state){
TextSpan(text: 'Round '),
TextSpan(text: '${game_state.round}', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)),
TextSpan(text: ' has begun.\n'),
TextSpan(text: '${game_state.names[game_state.turn]},',style: TextStyle(fontWeight: FontWeight.bold, fontSize: 28)),
TextSpan(text: '${game_state.players[game_state.turn].name},',style: TextStyle(fontWeight: FontWeight.bold, fontSize: 28)),
TextSpan(text: ' select your Chancellor for the upcoming election.'),
],
),
Expand Down Expand Up @@ -255,7 +256,7 @@ List <Widget> getUiState(context,width,height,GameState game_state){
text: TextSpan(
style:TextStyle(color:Colors.black,fontSize: 15),
children: <TextSpan>[
TextSpan(text: '${game_state.names[game_state.turn]},', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)),
TextSpan(text: '${game_state.players[game_state.turn].name},', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)),
TextSpan(text: ' in your esteemed role as the'),
TextSpan(text: ' President,',style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20)),
TextSpan(text: ' please select a policy card to be discarded.'),
Expand Down Expand Up @@ -469,7 +470,7 @@ List <Widget> getUiState(context,width,height,GameState game_state){
text: TextSpan(
style:TextStyle(color:Colors.black,fontSize: 20),
children: <TextSpan>[
TextSpan(text: '${game_state.names[game_state.turn]},', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 28)),
TextSpan(text: '${game_state.players[game_state.turn].name},', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 28)),
TextSpan(text: ' as the '),
TextSpan(text: 'President,',style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)),
TextSpan(text: ' you may now privately reveal the top three policy cards.'),
Expand Down Expand Up @@ -530,7 +531,7 @@ List <Widget> getUiState(context,width,height,GameState game_state){
text: TextSpan(
style:TextStyle(color:Colors.black),
children: <TextSpan>[
TextSpan(text: '${game_state.names[game_state.turn]}, as the President,', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18)),
TextSpan(text: '${game_state.players[game_state.turn].name}, as the President,', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18)),
TextSpan(text: " when you're ready, you have the option to eliminate a person. Removing Hitler would lead to a victory for the Liberals.\n"),
if(game_state.state=='kill_veto') TextSpan(text: "When both the President and Chancellor, both loyal to the Liberals, concur, the Veto option becomes viable."),
],
Expand Down Expand Up @@ -598,7 +599,7 @@ List <Widget> getUiState(context,width,height,GameState game_state){
text: TextSpan(
style:TextStyle(color:Colors.black,fontSize: 20),
children: <TextSpan>[
TextSpan(text: '${game_state.names[game_state.turn]}, as the President,', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)),
TextSpan(text: '${game_state.players[game_state.turn].name}, as the President,', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)),
TextSpan(text: ' you have the power to investigate the role of a person, whether they be a Fascist or a Liberal.'),

],
Expand Down Expand Up @@ -664,7 +665,7 @@ List <Widget> getUiState(context,width,height,GameState game_state){
text: TextSpan(
style:TextStyle(color:Colors.black, fontSize: 20),
children: <TextSpan>[
TextSpan(text: '${game_state.names[game_state.turn]}, as the President,', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)),
TextSpan(text: '${game_state.players[game_state.turn].name}, as the President,', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)),
TextSpan(text: 'you have the honor of selecting the next president!'),
],
),
Expand Down
2 changes: 1 addition & 1 deletion secret_hitler/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class _HomeState extends State<Home> {
context,
MaterialPageRoute(
builder: (context) =>
Game(name: gameState.names,roles:gameState.roles)),
Game()),
);
},
child: const Text('Continue previous game'),
Expand Down
Loading

0 comments on commit 2775339

Please sign in to comment.