/** Shows all hint words if game is not over */ private void showHint() { if (!game.isGameOver()) { String hint = getPossibleWords(); if (hint.length() > 1 && game.getWord().length() >= 3) text_message_view.setText("Possible words:\n" + hint); } }
/** * @return Returns string of possible words user can use to choose a symbol This is a hint words * for user */ private String getPossibleWords() { StringBuilder strbld = new StringBuilder("\n"); if (game.getWord().length() >= 3) { strbld.append("\n"); ArrayList<String> l = game.getPossibleWords(); for (String word : l) strbld.append(" " + word + "\n"); } return strbld.toString(); }
private void performGameOver() { String title; if (game.getWinner() == GhostGame.Winner.USER) { numberOfWins++; title = "Great! You won"; playGameWonMusic(); } else { numberOfLoses++; title = "Unfortunately, you lost"; playGameLostMusic(); } final Context context = this; AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); // set title alertDialogBuilder.setTitle(title); // set dialog message alertDialogBuilder .setMessage( "Current word: " + game.getWord() + "\nYou won " + numberOfWins + " time(s) and lost " + numberOfLoses + " time(s)") .setCancelable(false) .setPositiveButton( "Exit", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { MainActivity.this.finish(); } }) .setNegativeButton( "New Game", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); game = new GhostGame(words); setUpGUI(); } }); // create alert dialog AlertDialog alertDialog = alertDialogBuilder.create(); // show it alertDialog.show(); setUpGUI(); }
@Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); if ((savedInstanceState != null) && (savedInstanceState.getSerializable("game") != null)) { game = (GhostGame) savedInstanceState.getSerializable("game"); TextView textView1 = (TextView) findViewById(R.id.pressKey); TextView textView2 = (TextView) findViewById(R.id.theWord); TextView textView3 = (TextView) findViewById(R.id.text_message); textView1.setText(R.string.pressKey); textView2.setText(game.getWord()); if (!game.isGameOver() && hintOn) showHint(); else press_key_view.setText("Winner: " + game.getWinner().toString()); } }
/** * Called when the user clicks any of 25 a-z buttons on keyboard. Adds symbol to the word, checks * if game is not finished, sets text for labels * * @require symbol.length() == 1 * @ensure */ private void sendMessage(String userSymbol) { assert (userSymbol.length() == 1); String currWord = game.getWord(); String compSymbol = ""; if (game != null && !game.isGameOver()) { game.acceptUserLetter(userSymbol); compSymbol = game.getWord().charAt(game.getWord().length() - 1) + ""; if (game.isGameOver()) { text_message_view.setTextSize(20); press_key_view.setText("Winner: " + game.getWinner().toString()); performGameOver(); } else if (hintOn) { showHint(); } } else press_key_view.setText("Winner: " + game.getWinner()); text_word_view.setText(game.getWord()); // odun(userSymbol, currWord); // showSymbols(userSymbol+"", compSymbol,currWord); // show what user nad comp chose }