Resolve WES-187-unit-tests-hangman-and-minigame

This commit is contained in:
Jerome Coudron
2023-05-06 11:36:51 +00:00
committed by Jelle De Geest
parent b10358930b
commit c24fe037f6
16 changed files with 511 additions and 47 deletions

View File

@@ -28,7 +28,7 @@ public class HangmanController : AbstractMinigameController
/// <summary>
/// This integer holds the total amount of wrong guesses the player has made
/// </summary>
private int wrongs;
protected int wrongs;
/// <summary>
/// This integer holds the amount of correct letters of the word that the user has guessed
@@ -189,17 +189,17 @@ public class HangmanController : AbstractMinigameController
/// <summary>
/// Score obtained when guessing a correct letter
/// </summary>
private int correctLetterScore = 10;
public const int CORRECT_LETTER_SCORE = 10;
/// <summary>
/// Score obtained when guessing an incorrect letter
/// </summary>
private int incorrectLetterScore = -5;
public const int INCORRECT_LETTER_SCORE = -5;
/// <summary>
/// Score obtained when guessing the entire word
/// </summary>
private int winScore = 25;
public const int WIN_SCORE = 25;
/// <summary>
/// Set the AbstractMinigameController variable to inform it of the theme for the signPredictor
@@ -296,7 +296,6 @@ public class HangmanController : AbstractMinigameController
/// </summary>
public void SinglePlayer()
{
// This word is used for testing before dynamic word-fetching is added
PickRandomWord();
StartGame();
}
@@ -304,7 +303,7 @@ public class HangmanController : AbstractMinigameController
/// <summary>
/// Randomly select a word from a randomly selected theme, use this word for the hangman game for singleplayer.
/// </summary>
private void PickRandomWord()
public void PickRandomWord()
{
// Get a random index for the themes
// Then get a random index for a word to pull
@@ -336,7 +335,6 @@ public class HangmanController : AbstractMinigameController
StartGame();
}
}
/// <summary>
/// Update is called once per frame
/// </summary>
@@ -346,13 +344,7 @@ public class HangmanController : AbstractMinigameController
{
if (Input.GetKey(KeyCode.Backspace))
{
// Remove the last letter from the currentword
if (0 < currentWord.Length)
{
currentWord = currentWord[0..^1];
inputTextField.text = currentWord;
}
Input.ResetInputAxes();
BackSpacePressed();
}
gotoGameButton.SetActive(MIN_INC_WORD_LENGHT <= currentWord.Length);
@@ -379,7 +371,19 @@ public class HangmanController : AbstractMinigameController
});
}
}
/// <summary>
/// Functionality to be called when the backspace-key is pressed during input-mode
/// </summary>
public void BackSpacePressed()
{
// Remove the last letter from the currentword
if (0 < currentWord.Length)
{
currentWord = currentWord[0..^1];
inputTextField.text = currentWord;
}
Input.ResetInputAxes();
}
/// <summary>
/// Handles sign logic, so that it does not have to run every frame
/// This function is called when the UpdateFeedback has accepted a letter
@@ -413,7 +417,10 @@ public class HangmanController : AbstractMinigameController
break;
}
}
/// <summary>
/// Takes the currentSign and tries to enter it into the word if playing
/// When in input-mode it will just add the letter to the currentWord
/// </summary>
public void ConfirmAccept()
{
string letter = currentSign;
@@ -462,7 +469,9 @@ public class HangmanController : AbstractMinigameController
SwitchMode(1);
}
}
/// <summary>
/// The letter got rejected, start the letter-fetching process again
/// </summary>
public void ConfirmDeny()
{
confirmPanel.SetActive(false);
@@ -473,7 +482,10 @@ public class HangmanController : AbstractMinigameController
else if (mode == 4)
SwitchMode(1);
}
/// <summary>
/// Outside function to switch the modes this allows the gameIsactive-logic to be properly attached to the modes
/// </summary>
/// <param name="mode"></param>
public void SwitchMode(int mode)
{
this.mode = mode;
@@ -487,7 +499,6 @@ public class HangmanController : AbstractMinigameController
gameIsActive = false;
}
}
/// <summary>
/// Change the image that is being displayed
/// </summary>
@@ -501,10 +512,9 @@ public class HangmanController : AbstractMinigameController
hangmanImage.sprite = sprite;
scoreDisplay.text = $"Score: {CalculateScore()}";
scoreBonus.text = $"{incorrectLetterScore}";
scoreBonus.text = $"{INCORRECT_LETTER_SCORE}";
scoreBonus.color = new Color(0xf5 / 255.0f, 0x49 / 255.0f, 0x3d / 255.0f);
}
/// <summary>
/// In this function, the letters of the word selected in DisplayWord are updated after a correct guess.
/// </summary>
@@ -512,7 +522,6 @@ public class HangmanController : AbstractMinigameController
private void UpdateWord(string c)
{
int hits = 0;
for (int i = 0; i < currentWord.Length; i++)
{
if (currentWord[i] == c[0])
@@ -530,10 +539,9 @@ public class HangmanController : AbstractMinigameController
}
scoreDisplay.text = $"Score: {CalculateScore()}";
scoreBonus.text = $"+{hits * correctLetterScore}";
scoreBonus.text = $"+{hits * CORRECT_LETTER_SCORE}";
scoreBonus.color = new Color(0x8b / 255.0f, 0xd4 / 255.0f, 0x5e / 255.0f);
}
/// <summary>
/// This function returns the score that the user currently has
/// </summary>
@@ -541,11 +549,8 @@ public class HangmanController : AbstractMinigameController
public override int CalculateScore()
{
int won = corrects == currentWord.Length ? 1 : 0;
return corrects * correctLetterScore + wrongs * incorrectLetterScore + winScore * won;
return corrects * CORRECT_LETTER_SCORE + wrongs * INCORRECT_LETTER_SCORE + WIN_SCORE * won;
}
// The following functions originate from Spellingbee
/// <summary>
/// Delete all letter objects
/// </summary>
@@ -557,7 +562,6 @@ public class HangmanController : AbstractMinigameController
}
letters.Clear();
}
/// <summary>
/// Displays the word that needs to be spelled
/// </summary>
@@ -577,7 +581,6 @@ public class HangmanController : AbstractMinigameController
txt.text = c == ' ' ? "" : Char.ToString('_');
}
}
/// <summary>
/// The logic to process the signs sent by the signPredictor
/// </summary>
@@ -625,7 +628,6 @@ public class HangmanController : AbstractMinigameController
feedbackProgressImage.color = red;
}
}
// The logic for the internal workings of the game
if (accuracy > threshold)
{
@@ -667,7 +669,6 @@ public class HangmanController : AbstractMinigameController
timerCircle.fillAmount = currentTime;
}
}
/// <summary>
/// The logic to set the scoreboard of hangman
/// </summary>
@@ -692,7 +693,6 @@ public class HangmanController : AbstractMinigameController
score: CalculateScore()
);
}
/// <summary>
/// The hangman-specific logic that needs to be called at the start of the game
/// </summary>
@@ -723,4 +723,33 @@ public class HangmanController : AbstractMinigameController
DeleteWord();
}
// The following functions are only used for testing
public string getCurrentWord()
{
return currentWord;
}
public int getCurrentMode()
{
return mode;
}
public int getCorrects()
{
return corrects;
}
public int getWrongs()
{
return wrongs;
}
public string getUsedLetters()
{
return usedLettersText.text;
}
public void ProcessSignForTests(float accuracy, string sign)
{
ProcessMostProbableSign(accuracy, sign);
}
public float getCurrentTime()
{
return currentTime;
}
}

View File

@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
@@ -73,15 +70,11 @@ public class HangmanGameEndedPanel : AbstractGameEndedPanel
lettersWrongText.text = incorrectLetters.ToString();
lettersTotalText.text = (correctLetters + incorrectLetters).ToString();
// Accuracy
// Accuracy, should always be true
if (correctLetters + incorrectLetters > 0)
{
accuracyText.text = ((correctLetters) * 100f / (correctLetters + incorrectLetters)).ToString("#.##") + "%";
}
else
{
accuracyText.text = "-";
}
// Words
wordText.text = guessWord;