// drNim

//  compile with "swiftc main.swift"

//  run with "./main"

//

//  Created by Coty Ryan Miller on 9/30/17.

//

// code inspired by -- https://www.youtube.com/watch?v=9KABcmczPdg

//

// Download binary for mac -- http://cotyrmiller.com/code/DrNim/main

// Download Swift file     -- http://cotyrmiller.com/code/DrNim/main.swift

// Download Gameplay .mov  -- http://cotyrmiller.com/code/DrNim/DrNim.mov

//

var drNimTurn = false

let numMarbles = 12

var marbles = numMarbles

var marble = 0

var usrIpt: String?


var playerWon = false

var DrNimWon  = false

var GameStart = false

/************************************************************************

 * Clear screen fuction.

 ***********************************************************************/

func clear(times: Int) {

    for _ in 1...times {

        print(" ")

    }

}

/************************************************************************

 * Introduction screen and "cheater" switch.

 ***********************************************************************/

while GameStart == false

{

    //clear() // clear the screen yo!

    print("Welcome to Nim with... Dr Nim!")

    print("Dr Nim has a PhD at playing Nim, so he's gonna win!")

    print(" ")

    print("How would you like to play?")

    print("")

    print("1. Play like a grown up. ;)")

    print("2. DR NIM CHEATS! STOP CHEATING! T_T")

    print("3. ... What is this?")


    clear(times: 16)                   // clear the screen again yo!

    

    print("> ", terminator:"")// terminator says all on the same line

                            // because for some reason we don't just

                            // print and println!

    let cryOption = readLine()

    

    if cryOption == "1"{GameStart = true}

    

    if cryOption == "2" {

        marbles = marbles - 1

        marble = marble + 1

        GameStart = true

    }

    if cryOption == "3" {

        print("Dr Nim soft edition.")

        print(" ")

        print("    Written by Coty R Miller on 30/9/17")

        print(" ")

        print("    - What is this?")

        print("          Nim is a game in where you take turns removing coins or marbles ")

        print("      choosing between 1 and 3 marbles/coins unil the winner removes the")

        print("      until the winner removes the last remain coin.")

        print("")

        print("    - Can I actually beat this on the normal game mode?")

        print("          That's a good question! No... No you cant.")

        print("")

        print("    - Am I alive?")

        print("          I sure hope so!")

        print("")

        print(" ")

        print("")

        print("")

        print("")

        print("")

        print("")

        print("")

        print("                         ...PRESS RETURN TO CONTINUE ...")

        

        let RETURNTOSHIT = readLine()

    }

}

/************************************************************************

 * Actual game loop.

 ***********************************************************************/

repeat {

    // print the number of marbles... using asterisks... because fuck it.

    print("[\(marbles)]", terminator:"")

    for i in 1...marbles {

        print(" *", terminator:"") // terminator says all on the same line

                                  // because for some reason we don't just

                                  // print and println!

    }

    clear(times: 23)               // clear the screen again yo!

    print("[1..3] > ", terminator:"")

    /********************************************************************

     * Read player input and process it.

     *******************************************************************/

    usrIpt = readLine()

    if usrIpt == "1"

    {

        marbles = marbles - 1

        marble = marble + 1

        drNimTurn = true

    }

    if usrIpt == "2"

    {

        marbles = marbles - 2

        marble = marble + 2

        drNimTurn = true

    }

    if usrIpt == "3"

    {

        marbles = marbles - 3

        marble = marble + 3

        drNimTurn = true

    }

    if marble >= 4

    {

        marble = 0

    }

    if(marbles==0)

    {

        playerWon = true

        drNimTurn = false

    }

    /********************************************************************

     * Actuall DR Nim "algorithm"

     *******************************************************************/

    if drNimTurn == true

    {

        marbles = marbles - 1

        marble = marble + 1

        // Loop while marble > 1 && marble < 4

        repeat {

            // Is marble 1? If so exit.

            if marble == 1 {

            drNimTurn = false

            // If marble isn't 1 then...

            } else {

                // Is marble a 4? If so reset the marble counter and exit...

                if marble == 4

                {

                    marble = 0

                    drNimTurn = false

                }

                    // Otherwise take a marble and INC the counter.

                else {

                    marbles = marbles - 1

                    marble = marble + 1

                }

            }

            if(marbles==0)

            {

                DrNimWon = true

            }

        } while drNimTurn == true

        marble = 0

}


} while marbles != 0

/************************************************************************

 * End Game Screen!

 ***********************************************************************/

if DrNimWon == true

{

    print("You lost to Dr Nim!")

}

if playerWon == true

{

    print("You won against Dr Nim! To bad you had to cry to get here...")

}