Code Sketch


ai mind game
By: Mhalsakant School
Category: Programming
cleari()
drawStage(black)


// ============================================================
//                 AI MIND GAME
// ============================================================
// Mouse controlled
// No readLine
// 2D animation
// Rule based AI
// YES / NO questions
// Score
// New Game
// ============================================================


// ------------------------------------------------------------
// AI DATA
// ------------------------------------------------------------

case class Thing(
    name: String,
    animal: Boolean,
    food: Boolean,
    electronic: Boolean,
    living: Boolean,
    fourLegs: Boolean,
    big: Boolean,
    flies: Boolean,
    home: Boolean
)


// ------------------------------------------------------------
// AI KNOWLEDGE
// ------------------------------------------------------------

val things = List(

    Thing("DOG", true, false, false, true, true, false, false, true),

    Thing("CAT", true, false, false, true, true, false, false, true),

    Thing("LION", true, false, false, true, true, true, false, false),

    Thing("ELEPHANT", true, false, false, true, true, true, false, false),

    Thing("TIGER", true, false, false, true, true, true, false, false),

    Thing("DOLPHIN", true, false, false, true, false, true, false, false),

    Thing("EAGLE", true, false, false, true, false, false, true, false),

    Thing("FISH", true, false, false, true, false, false, false, true),

    Thing("PIZZA", false, true, false, false, false, false, false, true),

    Thing("APPLE", false, true, false, false, false, false, false, true),

    Thing("CAKE", false, true, false, false, false, false, false, true),

    Thing("BURGER", false, true, false, false, false, false, false, true),

    Thing("PHONE", false, false, true, false, false, false, false, true),

    Thing("COMPUTER", false, false, true, false, false, false, false, true),

    Thing("TELEVISION", false, false, true, false, false, false, false, true),

    Thing("BOOK", false, false, false, false, false, false, false, true),

    Thing("CAR", false, false, true, false, false, true, false, false),

    Thing("BICYCLE", false, false, false, false, false, false, false, false),

    Thing("AIRPLANE", false, false, true, false, false, true, true, false),

    Thing("BOAT", false, false, true, false, false, true, false, false),

    Thing("ROBOT", false, false, true, false, false, false, false, false),

    Thing("DOCTOR", false, false, false, true, false, false, false, false),

    Thing("TEACHER", false, false, false, true, false, false, false, false),

    Thing("SUPERMAN", false, false, false, true, false, false, true, false)
)


// ------------------------------------------------------------
// AI QUESTIONS
// ------------------------------------------------------------

case class Question(
    text: String,
    check: Thing => Boolean
)

val aiQuestions = Vector(

    Question(
        "Is it an animal?",
        x => x.animal
    ),

    Question(
        "Can you eat it?",
        x => x.food
    ),

    Question(
        "Is it electronic?",
        x => x.electronic
    ),

    Question(
        "Is it alive?",
        x => x.living
    ),

    Question(
        "Does it have four legs?",
        x => x.fourLegs
    ),

    Question(
        "Is it bigger than a person?",
        x => x.big
    ),

    Question(
        "Can it fly?",
        x => x.flies
    ),

    Question(
        "Is it normally found at home?",
        x => x.home
    )
)


// ------------------------------------------------------------
// GAME STATE
// ------------------------------------------------------------

var possible = things

var qNumber = 0

var score = 0

var active = true

var finished = false


// ------------------------------------------------------------
// TITLE
// ------------------------------------------------------------

val title =
    penColor(cm.cyan) ->
    Picture.text("AI MIND GAME")

title.setPosition(-100, 230)

draw(title)


// ------------------------------------------------------------
// INSTRUCTION
// ------------------------------------------------------------

val instruction =
    penColor(white) ->
    Picture.text("Think of one thing. Click YES or NO.")

instruction.setPosition(-180, 195)

draw(instruction)


// ------------------------------------------------------------
// AI ROBOT
// ------------------------------------------------------------

val robot = Picture {

    setPenColor(cm.cyan)

    setFillColor(cm.blue)

    setPenThickness(4)

    circle(60)

    setFillColor(white)

    setPosition(-22, 15)

    circle(10)

    setPosition(22, 15)

    circle(10)

    setFillColor(black)

    setPosition(-22, 15)

    circle(4)

    setPosition(22, 15)

    circle(4)

    setPenColor(white)

    setPosition(-25, -20)

    setHeading(0)

    forward(50)

    setPosition(0, 60)

    setPenColor(cm.yellow)

    setFillColor(cm.yellow)

    circle(8)
}

robot.setPosition(0, 75)

draw(robot)


// ------------------------------------------------------------
// AI STATUS
// ------------------------------------------------------------

val aiStatus =
    penColor(cm.green) ->
    Picture.text("AI ONLINE")

aiStatus.setPosition(-50, -10)

draw(aiStatus)


// ------------------------------------------------------------
// QUESTION BACKGROUND
// ------------------------------------------------------------

val questionBackground =
    Picture.rectangle(500, 90)

questionBackground.setPosition(-250, -170)

questionBackground.setFillColor(cm.darkGray)

questionBackground.setPenColor(cm.cyan)

draw(questionBackground)


// ------------------------------------------------------------
// QUESTION
// ------------------------------------------------------------

var questionText =
    penColor(white) ->
    Picture.text("Starting...")

questionText.setPosition(-100, -140)

draw(questionText)


// ------------------------------------------------------------
// YES BUTTON
// ------------------------------------------------------------

val yesButton =
    Picture.rectangle(180, 60)

yesButton.setPosition(-210, -90)

yesButton.setFillColor(cm.green)

yesButton.setPenColor(white)

draw(yesButton)


val yesLabel =
    penColor(white) ->
    Picture.text("YES")

yesLabel.setPosition(-160, -70)

draw(yesLabel)


// ------------------------------------------------------------
// NO BUTTON
// ------------------------------------------------------------

val noButton =
    Picture.rectangle(180, 60)

noButton.setPosition(30, -90)

noButton.setFillColor(cm.red)

noButton.setPenColor(white)

draw(noButton)


val noLabel =
    penColor(white) ->
    Picture.text("NO")

noLabel.setPosition(85, -70)

draw(noLabel)


// ------------------------------------------------------------
// SCORE
// ------------------------------------------------------------

var scoreText =
    penColor(cm.yellow) ->
    Picture.text("SCORE: 0")

scoreText.setPosition(-260, 160)

draw(scoreText)


// ------------------------------------------------------------
// QUESTION COUNTER
// ------------------------------------------------------------

var numberText =
    penColor(cm.yellow) ->
    Picture.text("QUESTION: 0")

numberText.setPosition(100, 160)

draw(numberText)


// ------------------------------------------------------------
// RESULT
// ------------------------------------------------------------

var resultText =
    penColor(cm.cyan) ->
    Picture.text("AI READY")

resultText.setPosition(-60, 15)

draw(resultText)


// ------------------------------------------------------------
// NEW GAME BUTTON
// ------------------------------------------------------------

val newButton =
    Picture.rectangle(180, 55)

newButton.setPosition(-90, -235)

newButton.setFillColor(cm.blue)

newButton.setPenColor(white)

draw(newButton)


val newLabel =
    penColor(white) ->
    Picture.text("NEW GAME")

newLabel.setPosition(-65, -215)

draw(newLabel)


// ------------------------------------------------------------
// UPDATE SCORE
// ------------------------------------------------------------

def updateScore() {

    scoreText.erase()

    scoreText =
        penColor(cm.yellow) ->
        Picture.text(
            "SCORE: " + score
        )

    scoreText.setPosition(-260, 160)

    draw(scoreText)
}


// ------------------------------------------------------------
// UPDATE QUESTION
// ------------------------------------------------------------

def updateQuestionNumber() {

    numberText.erase()

    numberText =
        penColor(cm.yellow) ->
        Picture.text(
            "QUESTION: " + qNumber
        )

    numberText.setPosition(100, 160)

    draw(numberText)
}


// ------------------------------------------------------------
// SHOW QUESTION
// ------------------------------------------------------------

def showQuestion() {

    questionText.erase()

    if (
        qNumber < aiQuestions.length &&
        possible.size > 1 &&
        !finished
    ) {

        questionText =
            penColor(white) ->
            Picture.text(
                aiQuestions(qNumber).text
            )

        questionText.setPosition(-180, -140)

        draw(questionText)

        resultText.erase()

        resultText =
            penColor(cm.cyan) ->
            Picture.text(
                "AI IS THINKING..."
            )

        resultText.setPosition(-95, 15)

        draw(resultText)

        active = true

    } else {

        finishGame()
    }
}


// ------------------------------------------------------------
// ANSWER YES
// ------------------------------------------------------------

def yesAnswer() {

    if (active && !finished) {

        active = false

        val current =
            aiQuestions(qNumber)

        possible =
            possible.filter(
                current.check
            )

        score = score + 10

        qNumber = qNumber + 1

        updateScore()

        updateQuestionNumber()

        showQuestion()
    }
}


// ------------------------------------------------------------
// ANSWER NO
// ------------------------------------------------------------

def noAnswer() {

    if (active && !finished) {

        active = false

        val current =
            aiQuestions(qNumber)

        possible =
            possible.filter(
                x => !current.check(x)
            )

        score = score + 5

        qNumber = qNumber + 1

        updateScore()

        updateQuestionNumber()

        showQuestion()
    }
}


// ------------------------------------------------------------
// FINISH
// ------------------------------------------------------------

def finishGame() {

    finished = true

    active = false

    questionText.erase()

    resultText.erase()

    if (possible.size == 1) {

        val answer =
            possible.head.name

        questionText =
            penColor(cm.yellow) ->
            Picture.text(
                "MY GUESS: " + answer
            )

        questionText.setPosition(-130, -140)

        draw(questionText)

        resultText =
            penColor(cm.green) ->
            Picture.text(
                "AI CONFIDENCE: HIGH"
            )

        resultText.setPosition(-110, 15)

        draw(resultText)

    } else {

        if (possible.size > 1) {

            val answer =
                possible.head.name

            questionText =
                penColor(cm.yellow) ->
                Picture.text(
                    "BEST GUESS: " + answer
                )

            questionText.setPosition(-145, -140)

            draw(questionText)

            resultText =
                penColor(cm.cyan) ->
                Picture.text(
                    "POSSIBILITIES: " +
                    possible.size
                )

            resultText.setPosition(-100, 15)

            draw(resultText)

        } else {

            questionText =
                penColor(cm.red) ->
                Picture.text(
                    "YOU BEAT THE AI!"
                )

            questionText.setPosition(-120, -140)

            draw(questionText)

            resultText =
                penColor(cm.yellow) ->
                Picture.text(
                    "AI NEEDS MORE KNOWLEDGE"
                )

            resultText.setPosition(-140, 15)

            draw(resultText)
        }
    }
}


// ------------------------------------------------------------
// YES BUTTON CLICK
// ------------------------------------------------------------

yesButton.onMouseClick { (x, y) =>

    yesAnswer()
}


// ------------------------------------------------------------
// NO BUTTON CLICK
// ------------------------------------------------------------

noButton.onMouseClick { (x, y) =>

    noAnswer()
}


// ------------------------------------------------------------
// NEW GAME
// ------------------------------------------------------------

newButton.onMouseClick { (x, y) =>

    possible = things

    qNumber = 0

    score = 0

    finished = false

    active = true

    questionText.erase()

    resultText.erase()

    updateScore()

    updateQuestionNumber()

    questionText =
        penColor(white) ->
        Picture.text(
            "New game started..."
        )

    questionText.setPosition(-125, -140)

    draw(questionText)

    resultText =
        penColor(cm.green) ->
        Picture.text(
            "AI RESET"
        )

    resultText.setPosition(-45, 15)

    draw(resultText)

    showQuestion()
}


// ------------------------------------------------------------
// 2D ANIMATION
// ------------------------------------------------------------

animate {

    robot.rotate(1)
}


// ------------------------------------------------------------
// START
// ------------------------------------------------------------

showQuestion()