Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
5da07f0
Tests
Jul 12, 2024
f98df9a
Merge pull request #6 from nonebula/playground
nonebula Jul 12, 2024
680da61
Tests
Jul 12, 2024
ad25235
Added planning notes for today's actions
nonebula Jul 15, 2024
8b7d7f5
Take2
Jul 15, 2024
7aca382
Take 3
Jul 15, 2024
9be750e
Testing suite debugging
nonebula Jul 15, 2024
569c5ea
Code for selecting character from gameboard added
nonebula Jul 15, 2024
d157660
GameLogic tweaked
nonebula Jul 15, 2024
abff432
Question handling refactored
nonebula Jul 15, 2024
44b8c65
Tests added
Jul 15, 2024
06490d1
Merging conflicts
nonebula Jul 15, 2024
30d7adf
Refactor attempted
nonebula Jul 15, 2024
ddb3df2
Merge pull request #7 from nonebula/type-mismatch-debug
nonebula Jul 15, 2024
3571f98
Team refactoring
Jul 15, 2024
69c1f53
Initialisation tests
Jul 15, 2024
8e7a773
Handles questions test
Jul 15, 2024
3fcdfcf
Testing tests
Jul 15, 2024
f856506
BoardSpec hair colour test fixed
Jul 15, 2024
151cb53
new tests added
Jul 15, 2024
6fb9000
File cleanup
nonebula Jul 16, 2024
9ad8fa4
Added type to characters
nonebula Jul 16, 2024
6fd6ad8
Test refactor
nonebula Jul 16, 2024
1496ec2
Presentation notes
nonebula Jul 16, 2024
2679ef7
Moved one note to bottom of board
nonebula Jul 16, 2024
1764ed0
few changes
Jul 16, 2024
8e71f0a
last commit
Jul 16, 2024
de39d12
Ext1 (#8)
nonebula Jul 16, 2024
7002bba
Minor styling
nonebula Jul 16, 2024
4507c39
Final touches
nonebula Jul 16, 2024
286cc46
Notes from presentation added
nonebula Jul 16, 2024
aab8b75
Character refactoring (Chunky)
nonebula Jul 23, 2024
851ee31
Board logic refactored
nonebula Jul 23, 2024
2c78d5c
Game object refactored
nonebula Jul 23, 2024
7060122
Game refactor
nonebula Jul 23, 2024
f313ae0
update
nonebula Jul 23, 2024
a28e99e
tests added
Jul 23, 2024
c90cb80
Test edit
nonebula Jul 23, 2024
c0cc4c5
EyeColor.Blue
Jul 23, 2024
31d73e5
Testing for new elements
nonebula Jul 23, 2024
432854f
Merge branch 'sharedmvp' of github.com:nonebula/GuessWho into sharedmvp
nonebula Jul 23, 2024
3ce5ae1
note added
nonebula Jul 23, 2024
9745878
Testing refinement (boardspec unfinished)
nonebula Jul 24, 2024
28c3fa4
space
nonebula Sep 9, 2024
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: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ lazy val root = (project in file("."))
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.2.19" % Test,
"org.scalactic" %% "scalactic" % "3.2.19",
)
)
73 changes: 70 additions & 3 deletions src/main/scala/GuessWhoGame/Board.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,74 @@
package GuessWhoGame

case class Board (Characters: List[Character]) {
def printCharacterNames(): Unit = {
Characters.foreach(Character => println(Character.name))
import scala.util.Random
import GuessWhoGame.character._

class Board(val characters: List[Character], val selectedCharacter: Character) {
def printCharacterNames(): List[String] = {
characters.map(_.name)
}

def printRemainingCharacters(remainingCharacters: List[Character]): List[String] = {
remainingCharacters.map(_.name)
}

def getRemainingCharacters(remainingCharacters: List[Character]): List[Character] = remainingCharacters

def checkWinCondition(remainingCharacters: List[Character]): Boolean = remainingCharacters.size == 1

def handleQuestion(attribute: String, value: Either[String, Boolean], remainingCharacters: List[Character]): List[Character] = {
attribute match {
case "name" => filterByName(value, remainingCharacters)
case "gender" => filterByGender(value, remainingCharacters)
case "haircolor" => filterByHairColor(value, remainingCharacters)
case "eyecolor" => filterByEyeColor(value, remainingCharacters)
case "wearsglasses" => filterByWearGlasses(value, remainingCharacters)
case "hasfacialhair" => filterByFacialHair(value, remainingCharacters)
case _ =>
println("Invalid attribute. Try again.")
remainingCharacters
}
}

def filterByName(value: Either[String, Boolean], remainingCharacters: List[Character]): List[Character] = {
value match {
case Left(name) => remainingCharacters.filter(_.name.equalsIgnoreCase(name))
case _ => remainingCharacters
}
}

def filterByGender(value: Either[String, Boolean], remainingCharacters: List[Character]): List[Character] = {
value match {
case Left(gender) => remainingCharacters.filter(_.gender.toString.equalsIgnoreCase(gender))
case _ => remainingCharacters
}
}

def filterByHairColor(value: Either[String, Boolean], remainingCharacters: List[Character]): List[Character] = {
value match {
case Left(hairColor) => remainingCharacters.filter(_.hairColor.toString.equalsIgnoreCase(hairColor))
case _ => remainingCharacters
}
}

def filterByEyeColor(value: Either[String, Boolean], remainingCharacters: List[Character]): List[Character] = {
value match {
case Left(eyeColor) => remainingCharacters.filter(_.eyeColor.toString.equalsIgnoreCase(eyeColor))
case _ => remainingCharacters
}
}

def filterByWearGlasses(value: Either[String, Boolean], remainingCharacters: List[Character]): List[Character] = {
value match {
case Right(wearsGlasses) => remainingCharacters.filter(_.wearsGlasses.exists(_.value == wearsGlasses))
case _ => remainingCharacters
}
}

def filterByFacialHair(value: Either[String, Boolean], remainingCharacters: List[Character]): List[Character] = {
value match {
case Right(facialHair) => remainingCharacters.filter(_.facialHair.exists(_.value == facialHair))
case _ => remainingCharacters
}
}
}
4 changes: 0 additions & 4 deletions src/main/scala/GuessWhoGame/Character.scala

This file was deleted.

166 changes: 92 additions & 74 deletions src/main/scala/GuessWhoGame/Game.scala
Original file line number Diff line number Diff line change
@@ -1,80 +1,98 @@
package GuessWhoGame

import scala.util.Random
import scala.io.StdIn
import GuessWhoGame.character._
import scala.annotation.tailrec

object Game extends App {

val char1: Character = Character("joe", "male", "brown", "blue", wearsGlasses = false, facialHair = false)
val char2: Character = Character("muhammad", "male", "black", "brown", wearsGlasses = true, facialHair = true)
val char3: Character = Character("april", "female", "blonde", "blue", wearsGlasses = true, facialHair = false)
val char4: Character = Character("sally", "female", "blue", "green", wearsGlasses = true, facialHair = true)
val char5: Character = Character("spencer", "male", "brown", "brown", wearsGlasses = false, facialHair = true)
val char6: Character = Character("gemma", "female", "ginger", "Blue", wearsGlasses = true, facialHair = false)
val char7: Character = Character("jamie", "male", "none", "green", wearsGlasses = true, facialHair = true)
val char8: Character = Character("jessica", "female", "green", "green", wearsGlasses = false, facialHair = false)
val char9: Character = Character("bilal", "male", "brown", "green", wearsGlasses = false, facialHair = true)
val char10: Character = Character("lisa", "female", "black", "brown", wearsGlasses = true, facialHair = false)
val char11: Character = Character("tom", "male", "brown", "blue", wearsGlasses = true, facialHair = false)
val char12: Character = Character("cheryl", "female", "blonde", "green", wearsGlasses = false, facialHair = false)
val char13: Character = Character("arei", "male", "brown", "brown", wearsGlasses = false, facialHair = true)
val char14: Character = Character("kelly", "female", "none", "green", wearsGlasses = false, facialHair = false)
val char15: Character = Character("tayamul", "male", "brown", "blue", wearsGlasses = false, facialHair = false)
val char16: Character = Character("elaine", "female", "ginger", "blue", wearsGlasses = false, facialHair = true)
val char17: Character = Character("roshan", "male", "brown", "blue", wearsGlasses = true, facialHair = false)
val char18: Character = Character("patricia", "female", "purple", "yellow", wearsGlasses = false, facialHair = false)
val char19: Character = Character("dave", "male", "none", "yellow", wearsGlasses = false, facialHair = false)
val char20: Character = Character("bobbiana", "female", "Brown", "Blue", wearsGlasses = true, facialHair = false)

var gameBoard = Board(List(char1, char2, char3, char4, char5, char6, char7, char8, char9, char10, char11, char12, char13, char14, char15, char16, char17, char18, char19, char20))

gameBoard.printCharacterNames()


//Run example of the game
//Call methods

/** game start - random character selected (private), characterlist printed to screen
* yes / no question asked
* list filtered based on fulfilment of criteria
* list reprinted to screen
* loop through all options, filtering and reprinting each time
* trigger game end when length of list is 1 ------- could end anytime, defined/earlier in code
* */



// case class GuessWhoGame.Character(
// name: String,
// gender: String,
// hairColor: String,
// eyeColor: String,
// wearsGlasses: Boolean,
// facialHair: Boolean
// )









def createGameBoard(): Board = {
val characters = CharacterManager.createCharacters()
val selectedCharacter = CharacterManager.selectRandomCharacter(characters)
new Board(characters, selectedCharacter)
}

def startGame(board: Board): Unit = {
println("Welcome to the Guess Who game!")
println("Try to guess the character by asking some questions...")
println("Available characters:")
board.printCharacterNames().foreach(println)

@tailrec
def playGame(remainingCharacters: List[Character]): Unit = {
if (board.checkWinCondition(remainingCharacters)) {
println(s"\nCongratulations, You guessed correctly. The character was ${board.selectedCharacter.name}.")
} else {
println("\nRemaining Characters:")
board.printRemainingCharacters(remainingCharacters).foreach(println)

val newRemainingCharacters = askQuestion(remainingCharacters)
playGame(newRemainingCharacters)
}
}

def askQuestion(remainingCharacters: List[Character]): List[Character] = {
println("\nEnter an attribute (hairColor, eyeColor, gender, wearsGlasses, hasFacialHair) to guess or 'name' to guess the character:")
val attribute = StdIn.readLine().toLowerCase()
attribute match {
case "haircolor" =>
println("Enter the hair color:")
val hairColor = StdIn.readLine().toLowerCase()
board.handleQuestion(attribute, Left(hairColor), remainingCharacters)
case "eyecolor" =>
println("Enter the eye color:")
val eyeColor = StdIn.readLine().toLowerCase()
board.handleQuestion(attribute, Left(eyeColor), remainingCharacters)
case "gender" =>
println("Enter the gender (male/female):")
val gender = StdIn.readLine().toLowerCase()
board.handleQuestion(attribute, Left(gender), remainingCharacters)
case "wearsglasses" =>
println("Does the character wear glasses? (true/false):")
val wearsGlasses = StdIn.readLine().toLowerCase().toBoolean
board.handleQuestion(attribute, Right(wearsGlasses), remainingCharacters)
case "hasfacialhair" =>
println("Does the character have facial hair? (true/false):")
val hasFacialHair = StdIn.readLine().toLowerCase().toBoolean
board.handleQuestion(attribute, Right(hasFacialHair), remainingCharacters)
case "name" =>
println("Enter the name of the character:")
val nameInput = StdIn.readLine().toLowerCase()
val newRemainingCharacters = board.handleQuestion(attribute, Left(nameInput), remainingCharacters)
if (board.checkWinCondition(newRemainingCharacters) && board.selectedCharacter.name.equalsIgnoreCase(nameInput)) {
println(s"\nCongratulations, You guessed correctly. The character was ${board.selectedCharacter.name}.")
} else {
println(s"\nGame over, you guessed incorrectly. The character was ${board.selectedCharacter.name}.")
}
newRemainingCharacters
case _ =>
println("Invalid attribute. Try again.")
remainingCharacters
}
}

@tailrec
def replayOrExit(): Unit = {
println("\nDo you want to play again? (yes/no)")
val playAgain = StdIn.readLine().toLowerCase()
playAgain match {
case "yes" | "y" =>
println("Starting a new game...")
val newBoard = createGameBoard()
startGame(newBoard)
case "no" | "n" =>
println("Thanks for playing Guess Who!")
case _ =>
println("Invalid response. Please type 'yes' or 'no'.")
replayOrExit()
}
}

playGame(board.characters)
replayOrExit()
}

val initialBoard = createGameBoard()
startGame(initialBoard)
}




//package Week2.Monday.AfternoonTaskSolution
//
//object Sanctuary extends App {
//
// def feedAnimal(animal: Animal) = println(s"You fed ${animal.name} some ${animal.dietType.eats}!")
//
// val hedwigTheSnowyOwl = new Owl("Hedwig", "Snowy Owl", 8, 2, "White", 150)
// val pumbaTheWarthog = new Warthog("Pumbaa", "Common Warthog", 4, "Brown", true)
// val jeffTheDragonfly = new Dragonfly("Jeff", "Blue Eyed Darner", 1, 12, 10, 1)
//
// feedAnimal(hedwigTheSnowyOwl)
//
//}

20 changes: 0 additions & 20 deletions src/main/scala/GuessWhoGame/GameBoard.scala

This file was deleted.

30 changes: 0 additions & 30 deletions src/main/scala/GuessWhoGame/GameLogic.scala

This file was deleted.

47 changes: 0 additions & 47 deletions src/main/scala/GuessWhoGame/GuessWhoGame.scala

This file was deleted.

Loading