Download hangman - FAU Android

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Design Project 2
22/20
Timur Tavtilov
HANGMAN
(Android Game Application)
Department of Computer Science and Engineering
FAU 2009
Application Requirements
1. Overview
Hangman is one the world's most famous games.
User needs to guess a secret word by trying one letter at a time. If he guesses a letter that is in the
word, the letter will appear (if there are multiple instances of the same letter they all will open). If the
player guesses wrong, when one of the body parts of the "hangman" will be drawn.
In easy mode - 6 errors
In medium - 5 errors
in hard
- 4 errors
The game ends when either the user guesses all the letters or all body parts are drawn.
2. Functionality
When the user opens the Hangman application he will see main menu with following choices:
- Continue
- New Game
- About
- Exit
Continue
New Game
About
Exit
will open previous game (if user decides to quit game while playing its state will
be saved)
will prompt user for difficulty level ( Easy - Medium - Hard ). Once the user
chooses difficulty level new game will start
will open new view with information about this game (instructions and options)
will exit the application
Once Game starts dependent on difficulty level user will see certain number of hangman parts
already drawn. Also user will see a hint helping him to guess a hidden word.
There is a EditText view which will check for any keyboard input. In addition, user can use
virtual keyboard. Also there is the functionality that if user inputs a letter, the Hangman
application will not count this letter again. (For example user can input same letter by mistake)
There is the “entered” text which will show all letters user already entered.
3. Structure
The simple mock-ups of the all screens attached.
4. References
[1] - http://developer.android.com/guide/developing/tools/emulator.html#skins.
[2] - http://developer.android.com/index.html.
[3] - http://developer.android.com/reference/packages.html.
Prototype view of “Main menu” and “Game” screens
Hangman Application
Final Look and Feel
Icon
Main Menu
Difficulty Selection
Easy Level Game Start
Medium Level Game Start
Hard Level Game Start
Game
Game End
Winner Screen
About Screen
Activity Diagrams
Sequence Diagrams
Diagrams show pressing “New Game”, “Continue”, and Game Cases.
Also at the end I added top-level diagrams
Starting “New
Game”
“Continue”
Game
Open Hangman Activity
Exit
Diagrams show pressing “New Game”, “Continue”, and Game Cases.
Starting “New Game”
“Continue”
Game
About Dialog
Source Code
Hangman.java
package edu.cse.fau.ttavtilo;
import
import
import
import
import
import
import
android.app.Activity;
android.app.AlertDialog;
android.content.DialogInterface;
android.content.Intent;
android.os.Bundle;
android.view.View;
android.widget.Button;
public class Hangman extends Activity {
//initiating buttons
Button btnContinue;
Button btnNewGame;
Button btnAbout;
Button btnExit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnContinue = (Button)findViewById(R.id.continue_button);
btnNewGame = (Button)findViewById(R.id.new_button);
btnAbout = (Button)findViewById(R.id.about_button);
btnExit = (Button)findViewById(R.id.exit_button);
//creating click listeners
btnContinue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// -1 means that Game Activity will load all its data from
Preferences
startGame(-1);
}
});
btnNewGame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// user needs to select a difficulty level
new AlertDialog.Builder(Hangman.this).setTitle("Select Difficulty")
.setItems(R.array.difficulty, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i) {
// calling startGame function which will lauch Game with
appropriate difficulty level
startGame(i);
}
})
.show();
}
});
btnAbout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent aboutIntent = new Intent(Hangman.this,
edu.cse.fau.ttavtilo.About.class);
Hangman.this.startActivity(aboutIntent);
}
});
btnExit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
private void startGame(int i) {
Intent intent = new Intent(Hangman.this, edu.cse.fau.ttavtilo.Game.class);
intent.putExtra(Game.KEY_DIFFICULTY, i);
startActivity(intent);
}
}
Game.java
package edu.cse.fau.ttavtilo;
import java.util.ArrayList;
import
import
import
import
import
import
import
import
import
import
import
import
import
import
android.app.Activity;
android.app.AlertDialog;
android.content.DialogInterface;
android.content.Intent;
android.content.SharedPreferences;
android.os.Bundle;
android.view.KeyEvent;
android.view.View;
android.view.View.OnKeyListener;
android.widget.Button;
android.widget.EditText;
android.widget.LinearLayout;
android.widget.TextView;
android.widget.Toast;
import java.util.Random;
public class Game extends Activity{
public static String KEY_DIFFICULTY;
public static final String PREFS_NAME = "Hangman_pref";
TextView hiddenText;
TextView hintText;
TextView enteredText;
EditText input;
LinearLayout background;
Button backBtn;
Button newGameBtn;
// text variable holds String representation of the hidden word
String text;
// hint
String hint;
// holds a String of all letters already inputed by user
String entered = "";
//count of errors
int count;
//difficulty level
//
0 for easy
//
1 for medium
//
2 for hard
//
-1 for Continue (loading saved data from previous game into main variables (text,
entered, count)
int diff;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
//getting difficulty level
diff = getIntent().getIntExtra(KEY_DIFFICULTY, -1);
//getting new hidden word
String texthint = getText();
String[] pair = texthint.split("#");
text = pair[0];
hint = pair[1];
//assembling UI
hintText = (TextView)findViewById(R.id.hint_txt);
hintText.setText(hint);
hiddenText = (TextView)findViewById(R.id.hidden_txt);
hiddenText.setText(encodeString(text, "*"));
enteredText = (TextView)findViewById(R.id.entered_txt);
enteredText.setText(entered);
background = (LinearLayout)findViewById(R.id.background);
count = setDifficulty(diff); //setting count
changeImage(count); // changing background
//setting input
input = (EditText)findViewById(R.id.input_txt);
input.setFocusable(true);
input.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
String temp;
String newLetter = "&";
newLetter = input.getText().toString();
temp = (String)enteredText.getText();
if(temp.indexOf(newLetter.toUpperCase()) >= 0) {
input.setText("");
return false;
}
input.setText(""); //clearing input
entered += newLetter.toUpperCase(); //adding inputed letter to the
entered String
enteredText.setText(temp + newLetter.toUpperCase());
hiddenText.setText(encodeString(text, newLetter.toUpperCase()));
//checking for win case
if(win(text)) {
Toast.makeText(getApplicationContext(),
"Congratulations!\n\tYou won!", Toast.LENGTH_LONG).show();
input.setFocusable(false);
changeImage(999);
}
//if not found in the hidden word decrement count
if(text.indexOf(newLetter.toUpperCase()) == -1) {
count--;
changeImage(count); //also change background (draw new
part)
}
//game over if there is no chances left
if(count == 0) {
Toast.makeText(getApplicationContext(), "Game Over",
Toast.LENGTH_LONG).show();
hiddenText.setText(encodeString(text, "#")); //display
hidden word
input.setFocusable(false); //no more guesses
}
return false;
}
});
backBtn = (Button)findViewById(R.id.back_button);
backBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
newGameBtn = (Button)findViewById(R.id.new_game_button);
newGameBtn.setOnClickListener(new View.OnClickListener() {
//creating new Game
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Starting a New Game",
Toast.LENGTH_SHORT).show();
new AlertDialog.Builder(Game.this).setTitle("Select Difficulty")
.setItems(R.array.difficulty, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i) {
startGame(i);
}
})
.show();
}
});
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
// Save the current game
String old_entered = this.entered;
String old_text = this.text;
String old_hint = this.hint;
int old_count = this.count;
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("old_text", old_text);
editor.putString("old_entered", old_entered);
editor.putString("old_hint", old_hint);
editor.putInt("old_count", old_count);
editor.commit();
getIntent().putExtra(KEY_DIFFICULTY, -1);
//putting diff to -1 ensures that if Game pauses it will start from the saved state
}
private String encodeString( String target, String letter) {
String result = "";
if(letter.contains("*")) { //this case will completely hide all letters
for(int i = 0; i < target.length(); i++) {
result += "_ "; //there is a space bar between the letters
}
return result;
}
else if(letter.contains("#")) { //this case will open all letters (happens when
user losses a game
for(int i = 0; i < target.length(); i++) {
result += target.charAt(i) + " ";
}
return result;
}
else { //this case will open all instances of the correct letter
for(int i = 0; i < target.length(); i++) {
if(target.charAt(i) == letter.charAt(0)) {
result += target.charAt(i) + " ";
}
else if(entered.indexOf(target.charAt(i)) != -1) {
result += target.charAt(i) + " ";
}
else
result += "_ ";
}
return result;
}
}
private boolean win(String text) {
if(text.length() > 0 && entered.length() > 0) {
for(int i = 0; i < text.length(); i++) {
if(entered.indexOf(text.charAt(i)) == -1) {
return false;
}
}
return true;
}
return false;
}
private void changeImage(int count) {
switch (count) {
case 6:
background.setBackgroundResource(R.drawable.game0);
break;
case 5:
background.setBackgroundResource(R.drawable.game1);
break;
case 4:
background.setBackgroundResource(R.drawable.game2);
break;
case 3:
background.setBackgroundResource(R.drawable.game3);
break;
case 2:
background.setBackgroundResource(R.drawable.game4);
break;
case 1:
background.setBackgroundResource(R.drawable.game5);
break;
case 0:
background.setBackgroundResource(R.drawable.gamefinal);
break;
case 999:
background.setBackgroundResource(R.drawable.gamewin);
break;
default:
background.setBackgroundResource(R.drawable.game0);
break;
}
}
private int setDifficulty(int dif) {
switch(dif) {
case -1:
//diff is -1 load saved data
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String old_text = settings.getString("old_text", text);
String old_entered = settings.getString("old_entered", "");
String old_hint = settings.getString("old_hint", hint);
int old_count = settings.getInt("old_count", 6);
this.text = old_text;
this.entered = old_entered;
this.count = old_count;
this.hint = old_hint;
enteredText.setText(entered);
hintText.setText(hint);
if(entered.length() > 0) {
for(int i = 0; i < entered.length(); i++) {
hiddenText.setText(encodeString(text, entered.charAt(i) + ""));
}
}
changeImage(count);
return count;
case 0:
return 6;
case 1:
return 5;
case 2:
return 4;
default:
return 6;
}
}
private String getText() {
String result = "FLORIDA#USA";
// all hidden words
ArrayList<String> words = new ArrayList<String>();
words.add("FLORIDA#USA");
words.add("MINNEAPOLIS#USA");
words.add("PHILADELPHIA#USA");
words.add("INDIANAPOIS#USA");
words.add("JAKSONVILLE#USA");
words.add("WASHINGTON#USA");
words.add("SACRAMENTO#USA");
words.add("CAMBRIDGE#USA");
words.add("AFGHANISTAN#Country");
words.add("BAHAMAS#Country");
words.add("CAMBODIA#Country");
words.add("GERMANY#Country");
words.add("UZBEKISTAN#Country");
words.add("MOZAMBIQUE#Country");
words.add("BRASILIA#Capital City");
words.add("OTTAWA#Capital City");
words.add("MOSCOW#Capital City");
words.add("KINGSTON#Capital City");
words.add("MONROVIA#Capital City");
words.add("ISLAMABAD#Capital City");
words.add("AMAZON#River");
words.add("YENISEI#River");
words.add("MEKONG#River");
words.add("MISSISSIPPI#River");
words.add("TOCANTINS#River");
words.add("COLORADO#River");
words.add("VOLGA#River");
words.add("EVEREST#Mountain");
words.add("LHOTSE#Mountain");
words.add("MAKALU#Mountain");
words.add("ELBRUS#Mountain");
words.add("KILIMANJARO#Mountain");
words.add("ANNAPURNA#Mountain");
words.add("ANAMUDI#Mountain");
words.add("MANAT#Currency");
words.add("DINAR#Currency");
words.add("AFGHANI#Currency");
words.add("KORUNA#Currency");
words.add("POUND#Currency");
words.add("SHILLING#Currency");
words.add("WINNIPEG#Lake");
words.add("MICHIGAN#Lake");
words.add("BAIKAL#Lake");
words.add("VICTORIA#Lake");
words.add("ISSYKKUL#Lake");
words.add("NIPIGON#Lake");
words.add("MANITOBA#Lake");
words.add("FEDMONTON#Canada");
words.add("CAMROSE#Canada");
words.add("CALGARY#Canada");
words.add("WETASKIVIN#Canada");
words.add("AIRDRIE#Canada");
words.add("LANGLEY#Canada");
words.add("ZAPOPAN#Mexico");
words.add("MONTERREY#Mexico");
words.add("TOLUCA#Mexico");
words.add("CANCUN#Mexico");
words.add("VERACRUZ#Mexico");
words.add("URUAPAN#Mexico");
words.add("MACUSPANA#Mexico");
Random generator = new Random();
int randomIndex = generator.nextInt( words.size() );
result = words.get(randomIndex);
return result;
}
private void startGame(int i) {
Intent intent = new Intent(Game.this, edu.cse.fau.ttavtilo.Game.class);
intent.putExtra(Game.KEY_DIFFICULTY, i);
startActivity(intent);
}
}
About.java
package edu.cse.fau.ttavtilo;
import
import
import
import
android.app.Activity;
android.os.Bundle;
android.view.View;
android.widget.Button;
public class About extends Activity {
Button btnExit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
btnExit = (Button)findViewById(R.id.exit_button);
btnExit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
about.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="@drawable/game6"
android:padding="5pt"
android:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/about_txt"
android:textColor="@color/white"
android:background="@color/about_bg"
android:layout_marginTop="10pt"
android:padding="5pt"
android:typeface="serif"
android:textSize="8pt"/>
<Button
android:id="@+id/exit_button"
android:layout_width="fill_parent"
android:layout_height="20pt"
android:text="@string/back_btn"
android:background="@color/transparent"
android:layout_marginTop="15pt"
android:layout_marginLeft="20pt"
android:layout_marginRight="20pt"
android:textStyle="bold"
android:textColor="@color/white"
android:typeface="serif"
android:textSize="11pt"/>
</LinearLayout>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="@drawable/hangman"
android:padding="30dip"
android:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center">
<Button
android:id="@+id/continue_button"
android:layout_width="fill_parent"
android:layout_height="20pt"
android:text="@string/continue_btn"
android:background="@color/transparent"
android:layout_marginTop="5pt"
android:textStyle="bold"
android:textColor="@color/white"
android:typeface="serif"
android:textSize="11pt"/>
<Button
android:id="@+id/new_button"
android:layout_width="fill_parent"
android:layout_height="20pt"
android:text="@string/new_game_btn"
android:background="@color/transparent"
android:layout_marginTop="5pt"
android:textStyle="bold"
android:textColor="@color/white"
android:typeface="serif"
android:textSize="11pt"/>
<Button
android:id="@+id/about_button"
android:layout_width="fill_parent"
android:layout_height="20pt"
android:text="@string/about_btn"
android:background="@color/transparent"
android:layout_marginTop="5pt"
android:textStyle="bold"
android:textColor="@color/white"
android:typeface="serif"
android:textSize="11pt"/>
<Button
android:id="@+id/exit_button"
android:layout_width="fill_parent"
android:layout_height="20pt"
android:text="@string/exit_btn"
android:background="@color/transparent"
android:layout_marginTop="5pt"
android:textStyle="bold"
android:textColor="@color/white"
android:typeface="serif"
android:textSize="11pt"/>
</LinearLayout>
</LinearLayout>
game.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/background"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="@drawable/game0"
android:padding="5dip"
android:orientation="vertical">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="@color/transparent"
android:layout_marginTop="10pt"
android:padding="5dip"
android:orientation="vertical">
<TextView
android:id="@+id/hidden_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:text=""
android:gravity="center"
android:textStyle="bold"
android:textColor="@color/white"
android:typeface="serif"
android:textSize="12pt"/>
<TextView
android:id="@+id/hint_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2pt"
android:textStyle="bold"
android:background="@color/transparent"
android:text=""
android:gravity="center"
android:textColor="@color/white"
android:textSize="6pt"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="@color/transparent"
android:layout_marginTop="90pt"
android:padding="5dip"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center">
<TextView
android:layout_width="100pt"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:text="YOUR NEXT GUESS IS: "
android:textStyle="bold"
android:gravity="center"
android:textColor="@color/white"
android:textSize="9pt"/>
<EditText
android:id="@+id/input_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:textStyle="bold"
android:gravity="center"
android:textColor="@color/black"
android:textSize="9pt"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="5pt"
android:layout_gravity="left">
<TextView
android:layout_width="60pt"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:text="Already entered: "
android:textStyle="bold"
android:gravity="center"
android:textColor="@color/white"
android:textSize="6pt"/>
<TextView
android:id="@+id/entered_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:textStyle="bold"
android:gravity="left"
android:textColor="@color/white"
android:textSize="6pt"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center">
<Button
android:id="@+id/new_game_button"
android:layout_width="62pt"
android:layout_height="20pt"
android:text="@string/new_game_btn"
android:background="@color/transparent"
android:layout_marginTop="5pt"
android:textStyle="bold"
android:textColor="@color/white"
android:typeface="serif"
android:textSize="11pt"/>
<Button
android:id="@+id/back_button"
android:layout_width="62pt"
android:layout_height="20pt"
android:text="@string/back_btn"
android:background="@color/transparent"
android:layout_marginTop="5pt"
android:layout_marginLeft="11pt"
android:textStyle="bold"
android:textColor="@color/white"
android:typeface="serif"
android:textSize="11pt"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#FFFFFF</color>
<color name="black">#000000</color>
<color name="transparent">#55000000</color>
<color name="text_red">#d2040e</color>
<color name="about_bg">#77336699</color>
</resources>
difficulty.xml
<resources>
<array name="difficulty">
<item>@string/easy_label</item>
<item>@string/medium_label</item>
<item>@string/hard_label</item>
</array>
</resources>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Hangman!</string>
<string name="app_name">Hangman</string>
<string name="continue_btn">Continue</string>
<string name="new_game_btn">New Game</string>
<string name="about_btn">About</string>
<string name="exit_btn">Exit</string>
<string name="about_txt">Hangman is a word game in which Android selects a word that the
player must guess by supplying each of its letters: for each incorrect guess a part of a figure
of a hanged man is drawn.\n\nThe player wins if he opens all letters of a hidden word before all
parts of Hangman are drawn. Overvise he losses.</string>
<string name="back_btn">Back</string>
<string name="easy_label">Easy (6 parts)</string>
<string name="medium_label">Medium (5 parts)</string>
<string name="hard_label">Hard (4 parts)</string>
</resources>
Related documents