Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first commit #56

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Flutter.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,5 @@ Submission has to be done by sharing your github repo link and the .apk file of
- Make a Pull request

## Submissions
repo:- https://github.com/Yash-Agarwal1708/CSOC22-Week1.git
.apk file:- https://drive.google.com/drive/folders/1ZLoLLxurfCRyIn_tm6dop0UVoR8PeXlx
Binary file added app-release.apk
Binary file not shown.
Binary file added data_repo/img/progress_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data_repo/img/progress_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data_repo/img/progress_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data_repo/img/progress_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data_repo/img/progress_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data_repo/img/progress_5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data_repo/img/progress_6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data_repo/img/progress_7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data_repo/img/victory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
92 changes: 92 additions & 0 deletions lib/engine/hangman.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import 'dart:async';

class HangmanGame {
static const int hanged = 7; // number of wrong guesses before the player's demise

final List<String> wordList; // list of possible words to guess
final Set<String> lettersGuessed = new Set<String>();

List<String> _wordToGuess;
int _wrongGuesses;

StreamController<Null> _onWin = new StreamController<Null>.broadcast();
Stream<Null> get onWin => _onWin.stream;

StreamController<Null> _onLose = new StreamController<Null>.broadcast();
Stream<Null> get onLose => _onLose.stream;

StreamController<int> _onWrong = new StreamController<int>.broadcast();
Stream<int> get onWrong => _onWrong.stream;

StreamController<String> _onRight = new StreamController<String>.broadcast();
Stream<String> get onRight => _onRight.stream;

StreamController<String> _onChange = new StreamController<String>.broadcast();
Stream<String> get onChange => _onChange.stream;

HangmanGame(List<String> words) : wordList = new List<String>.from(words);

void newGame() {
// shuffle the word list into a random order
wordList.shuffle();

// break the first word from the shuffled list into a list of letters
_wordToGuess = wordList.first.split('');

// reset the wrong guess count
_wrongGuesses = 0;

// clear the set of guessed letters
lettersGuessed.clear();

// declare the change (new word)
_onChange.add(wordForDisplay);
}

void guessLetter(String letter) {
// store guessed letter
lettersGuessed.add(letter);

// if the guessed letter is present in the word, check for a win
// otherwise, check for player death
if (_wordToGuess.contains(letter)) {
_onRight.add(letter);

if (isWordComplete) {
_onChange.add(fullWord);
_onWin.add(null);
}
else {
_onChange.add(wordForDisplay);
}
}
else {
_wrongGuesses++;

_onWrong.add(_wrongGuesses);

if (_wrongGuesses == hanged) {
_onChange.add(fullWord);
_onLose.add(null);
}
}
}

int get wrongGuesses => _wrongGuesses;
List<String> get wordToGuess => _wordToGuess;
String get fullWord => wordToGuess.join();

String get wordForDisplay => wordToGuess.map((String letter) =>
lettersGuessed.contains(letter) ? letter : "_").join();

// check to see if every letter in the word has been guessed
bool get isWordComplete {
for (String letter in _wordToGuess) {
if (!lettersGuessed.contains(letter)) {
return false;
}
}

return true;
}
}
35 changes: 35 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:flutter/material.dart';

import 'package:hangman/engine/hangman.dart';
import 'package:hangman/ui/hangman_page.dart';

const List<String> wordList = const ["PLENTY","ACHIEVE","CLASS","STARE","AFFECT","THICK","CARRIER","BILL","SAY","ARGUE","OFTEN","GROW","VOTING","SHUT","PUSH","FANTASY","PLAN","LAST","ATTACK","COIN","ONE","STEM","SCAN","ENHANCE","PILL","OPPOSED","FLAG","RACE","SPEED","BIAS","HERSELF","DOUGH","RELEASE","SUBJECT","BRICK","SURVIVE","LEADING","STAKE","NERVE","INTENSE","SUSPECT","WHEN","LIE","PLUNGE","HOLD","TONGUE","ROLLING","STAY","RESPECT","SAFELY"];

void main() => runApp(HangmanApp());

class HangmanApp extends StatefulWidget {
@override
State<StatefulWidget> createState() => _HangmanAppState();
}

class _HangmanAppState extends State<HangmanApp> {
HangmanGame _engine;

@override
void initState() {
super.initState();
_engine = HangmanGame(wordList);
}

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Hangman',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HangmanPage(_engine),
);
}
}
224 changes: 224 additions & 0 deletions lib/ui/hangman_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
import 'package:flutter/material.dart';

import 'package:hangman/engine/hangman.dart';

const List<String> progressImages = const [
'data_repo/img/progress_0.png',
'data_repo/img/progress_1.png',
'data_repo/img/progress_2.png',
'data_repo/img/progress_3.png',
'data_repo/img/progress_4.png',
'data_repo/img/progress_5.png',
'data_repo/img/progress_6.png',
'data_repo/img/progress_7.png',
];

const String victoryImage = 'data_repo/img/victory.png';

const List<String> alphabet = const [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
];

const TextStyle activeWordStyle = TextStyle(
fontSize: 30.0,
letterSpacing: 5.0,
);
class HangmanPage extends StatefulWidget {
final HangmanGame _engine;

HangmanPage(this._engine);

@override
State<StatefulWidget> createState() => _HangmanPageState();
}

class _HangmanPageState extends State<HangmanPage> {

bool _showPlayGame = true;
bool _showNewGame;
String _activeImage;
String _activeWord;

@override
void _playGame() {
widget._engine.newGame();

this.setState(() {
_activeWord = '';
_activeImage = progressImages[0];
_showPlayGame = false;
});
}
void _showInstructions() {
var _ins_text = 'Hangman is a simple word guessing game.\nPlayers try to figure out an unknown word by guessing letters.\nIf too many letters which do not appear in the word are guessed, the player loses.\n';
showDialog(
context: context,
builder: (context) {
return Dialog(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(40)),
elevation: 16,
child: Container(
height: 400.0,
width: 460.0,
child: ListView(
children: <Widget>[
SizedBox(height: 40),
Center(
child: Text(
_ins_text,
style: TextStyle(fontSize: 24, color: Colors.blue, fontWeight: FontWeight.bold,),
textAlign: TextAlign.center,
),
),
ElevatedButton(
child: Text('Main Menu',style: TextStyle(fontSize: 30),),
onPressed: () => Navigator.pop(context),
),
],
),
),
);
},
);
}
void initState() {
super.initState();

widget._engine.onChange.listen(this._updateWordDisplay);
widget._engine.onWrong.listen(this._updateGallowsImage);
widget._engine.onWin.listen(this._win);
widget._engine.onLose.listen(this._gameOver);

this._newGame();
}

void _updateWordDisplay(String word) {
this.setState(() {
_activeWord = word;
});
}

void _updateGallowsImage(int wrongGuessCount) {
this.setState(() {
_activeImage = progressImages[wrongGuessCount];
});
}

void _win([_]) {
this.setState(() {
_activeImage = victoryImage;
this._gameOver();
});
}

void _gameOver([_]) {
this.setState(() {
_showNewGame = true;
});
}

void _newGame() {
widget._engine.newGame();

this.setState(() {
_activeWord = '';
_activeImage = progressImages[0];
_showNewGame = false;
});
}

Widget _renderBottomContent() {
if (_showNewGame) {
return new Container(
child: Column(
children: <Widget>[
ElevatedButton(
child: Text('New Game'),
onPressed: this._newGame,
),
ElevatedButton(
child: Text('Instructions'),
onPressed: this._showInstructions,
),
],
),
);
}
else {
final Set<String> lettersGuessed = widget._engine.lettersGuessed;

return Wrap(
spacing: 1.0,
runSpacing: 1.0,
alignment: WrapAlignment.center,
children: alphabet.map((letter) =>
MaterialButton(
child: Text(letter),
padding: EdgeInsets.all(2.0),
onPressed: lettersGuessed.contains(letter) ? null : () {
widget._engine.guessLetter(letter);
},
)).toList(),
);
}
}

@override
Widget build(BuildContext context) {
if (_showPlayGame) {
return new Container(
child: Column(
children: <Widget>[
new Padding(
padding: const EdgeInsets.symmetric(vertical: 160.0),
),
ElevatedButton(
child: Text('Play Game',style: TextStyle(fontSize: 30),),
onPressed: this._playGame,
),
new Padding(
padding: const EdgeInsets.symmetric(vertical: 5.0),
),
ElevatedButton(
child: Text('Instructions',style: TextStyle(fontSize: 30),),
onPressed: this._showInstructions,
),
],
),
);
}
else {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('Hangman'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// Image
Expanded(
child: Image.asset(_activeImage),
),
// Word
Padding(
padding: EdgeInsets.all(2.0),
child: Center(
child: Text(_activeWord, style: activeWordStyle),
),
),
// Controls
Expanded(
child: Center(
child: this._renderBottomContent(),
),
),
],
),
),
);
}
}
}
Empty file added lib/ui/instructions_page.dart
Empty file.