Code Sketch


alex
By: Mhalsakant School
Category: Art
import scala.collection.mutable.ArrayBuffer
import scala.util.Random

clear()
originBottomLeft()

// ============================================================
// IPL MEGA AUCTION SIMULATOR - WITH COMPUTER RIVAL V6.0
// ============================================================

var globalTime = 0.0
var currentStage = 0 // 0: Welcome, 1: Instructions, 2: Game Play

// Game Variables
var myBudget: Int = 5000       // Your starting budget ?5000
var cpuBudget: Int = 5000      // Computer's starting budget ?5000
var mySquadCount: Int = 0
var cpuSquadCount: Int = 0
var currentPlayerIndex: Int = 0
var currentBid: Int = 0        // Current active bid price
var highestBidder: String = "" // "You", "CPU", or "None"

// Player Case Class
case class Player(name: String, role: String, basePrice: Int)

// Real IPL Players Master Pool
val masterPlayers = Array(
  Player("Virat Kohli", "Batsman", 600),
  Player("MS Dhoni", "Wicketkeeper", 550),
  Player("Rohit Sharma", "Batsman", 600),
  Player("Jasprit Bumrah", "Bowler", 650),
  Player("Hardik Pandya", "All-Rounder", 550),
  Player("Rashid Khan", "Bowler", 500),
  Player("Surya Kumar Yadav", "Batsman", 550),
  Player("Ravindra Jadeja", "All-Rounder", 500),
  Player("Sanju Samson", "Wicketkeeper", 450),
  Player("Shubman Gill", "Batsman", 500),
  Player("Rishabh Pant", "Wicketkeeper", 500),
  Player("KL Rahul", "Batsman", 450),
  Player("Yashasvi Jaiswal", "Batsman", 400),
  Player("Ishan Kishan", "Wicketkeeper", 400),
  Player("Bhuvneshwar Kumar", "Bowler", 350)
)

val playersList = ArrayBuffer.empty[Player]
val mySquad = ArrayBuffer.empty[Player]
val cpuSquad = ArrayBuffer.empty[Player]

// Function to reset and load random players
def generateRandomPlayers(): Unit = {
  playersList.clear()
  val shuffled = Random.shuffle(masterPlayers.toSeq)
  var i = 0
  while (i < 8) {
    playersList.append(shuffled(i))
    i += 1
  }
  if (playersList.length > 0) {
    currentBid = playersList(0).basePrice
    highestBidder = "None"
  }
}

generateRandomPlayers()

// Animation Loop
animateWithState(0.0) { angle =>
  erasePictures()
  globalTime += 0.03

  // Background
  setBackground(Color(10, 20, 35))

  if (currentStage == 0) {
    // --- STAGE 0: Welcome ---
    val title = Picture.text("IPL MEGA AUCTION SIMULATOR", 24)
    title.setPenColor(Color(255, 215, 0))
    title.setPosition(cwidth / 2.0 - 180, cheight - 120)
    draw(title)

    val sub = Picture.text("Battle against Computer AI with a budget of ?5000!", 15)
    sub.setPenColor(Color(200, 230, 255))
    sub.setPosition(cwidth / 2.0 - 190, cheight - 170)
    draw(sub)

    val prompt = Picture.text("Click anywhere or Press ENTER to Continue", 14)
    prompt.setPenColor(Color(255, 180, 50))
    prompt.setPosition(cwidth / 2.0 - 160, 80)
    draw(prompt)

  } else if (currentStage == 1) {
    // --- STAGE 1: Instructions ---
    val heading = Picture.text("=== AUCTION RULES & CONTROLS ===", 18)
    heading.setPenColor(Color(255, 220, 100))
    heading.setPosition(cwidth / 2.0 - 140, cheight - 70)
    draw(heading)

    val r1 = Picture.text("1. Budget: You (?5000) vs CPU (?5000)", 15)
    r1.setPenColor(Color(255, 255, 255))
    r1.setPosition(cwidth / 2.0 - 160, cheight - 120)
    draw(r1)

    val r2 = Picture.text("2. Press 'B' to Raise Bid | 'C' for CPU Bid", 15)
    r2.setPenColor(Color(255, 255, 255))
    r2.setPosition(cwidth / 2.0 - 160, cheight - 160)
    draw(r2)

    val r3 = Picture.text("3. Press 'S' to Sell & Move to Next Player", 15)
    r3.setPenColor(Color(255, 255, 255))
    r3.setPosition(cwidth / 2.0 - 160, cheight - 200)
    draw(r3)

    val startPrompt = Picture.text("Press ENTER or Click to START Auction!", 16)
    startPrompt.setPenColor(Color(50, 255, 100))
    startPrompt.setPosition(cwidth / 2.0 - 160, 60)
    draw(startPrompt)

  } else {
    // --- STAGE 2: Gameplay (The Auction Floor) ---
    
    // Top HUD: User & CPU Status
    val hudYou = Picture.text("Your Budget: ?" + myBudget + " | Squad: " + mySquadCount, 16)
    hudYou.setPenColor(Color(0, 255, 127))
    hudYou.setPosition(30, cheight - 40)
    draw(hudYou)

    val hudCpu = Picture.text("CPU Budget: ?" + cpuBudget + " | Squad: " + cpuSquadCount, 16)
    hudCpu.setPenColor(Color(255, 100, 100))
    hudCpu.setPosition(30, cheight - 70)
    draw(hudCpu)

    if (currentPlayerIndex < playersList.length) {
      val p = playersList(currentPlayerIndex)

      // Player Card Box
      val cardBox = Picture.rectangle(420, 190)
      cardBox.setPenColor(Color(255, 215, 0))
      cardBox.setPenThickness(2.0)
      cardBox.setPosition(cwidth / 2.0 - 210, cheight / 2.0 - 60)
      draw(cardBox)

      val nameTxt = Picture.text("Player: " + p.name, 20)
      nameTxt.setPenColor(Color(255, 255, 255))
      nameTxt.setPosition(cwidth / 2.0 - 190, cheight / 2.0 + 75)
      draw(nameTxt)

      val roleTxt = Picture.text("Role: " + p.role, 15)
      roleTxt.setPenColor(Color(150, 200, 255))
      roleTxt.setPosition(cwidth / 2.0 - 190, cheight / 2.0 + 40)
      draw(roleTxt)

      val priceTxt = Picture.text("Base Price: ?" + p.basePrice + " | Current Bid: ?" + currentBid, 15)
      priceTxt.setPenColor(Color(255, 165, 0))
      priceTxt.setPosition(cwidth / 2.0 - 190, cheight / 2.0 + 5)
      draw(priceTxt)

      val bidderColor = if (highestBidder == "You") Color(0, 255, 127) else if (highestBidder == "CPU") Color(255, 100, 100) else Color(200, 200, 200)
      val bidderTxt = Picture.text("Highest Bidder: " + highestBidder, 16)
      bidderTxt.setPenColor(bidderColor)
      bidderTxt.setPosition(cwidth / 2.0 - 190, cheight / 2.0 - 30)
      draw(bidderTxt)

      val controls = Picture.text("[B] Raise Bid (+50) | [C] CPU Bid | [S] Sell & Next", 13)
      controls.setPenColor(Color(200, 230, 255))
      controls.setPosition(cwidth / 2.0 - 190, cheight / 2.0 - 65)
      draw(controls)

    } else {
      // Auction Over
      val overText = Picture.text("=== AUCTION COMPLETED! ===", 22)
      overText.setPenColor(Color(255, 215, 0))
      overText.setPosition(cwidth / 2.0 - 160, cheight / 2.0 + 50)
      draw(overText)

      val finalScore = Picture.text("Your Players: " + mySquadCount + " | CPU Players: " + cpuSquadCount, 14)
      finalScore.setPenColor(Color(255, 255, 255))
      finalScore.setPosition(cwidth / 2.0 - 180, cheight / 2.0)
      draw(finalScore)
    }

    val hintText = Picture.text("Press R to Reset Auction", 14)
    hintText.setPenColor(Color(150, 150, 150))
    hintText.setPosition(30, 30)
    draw(hintText)
  }

  angle + 1.0
}

// Mouse Controls
onMouseClick { (x, y) =>
  if (currentStage == 0) {
    currentStage = 1
  } else if (currentStage == 1) {
    currentStage = 2
  }
}

// Key Controls for Bidding
onKeyPress { k =>
  if (currentStage < 2) {
    if (k == '\n' || k == '\r') {
      currentStage += 1
    }
  } else {
    if (currentPlayerIndex < playersList.length) {
      val p = playersList(currentPlayerIndex)

      if (k == 'b' || k == 'B') {
        // You raise the bid
        val nextBid = if (highestBidder == "None") p.basePrice else currentBid + 50
        if (myBudget >= nextBid) {
          currentBid = nextBid
          highestBidder = "You"
        }
      } else if (k == 'c' || k == 'C') {
        // Computer AI tries to counter bid
        val nextBid = currentBid + 50
        if (cpuBudget >= nextBid && highestBidder != "CPU") {
          currentBid = nextBid
          highestBidder = "CPU"
        }
      } else if (k == 's' || k == 'S') {
        // Sell player to the current highest bidder
        if (highestBidder == "You") {
          myBudget -= currentBid
          mySquad.append(p)
          mySquadCount += 1
        } else if (highestBidder == "CPU") {
          cpuBudget -= currentBid
          cpuSquad.append(p)
          cpuSquadCount += 1
        }
        // Move to next player
        currentPlayerIndex += 1
        if (currentPlayerIndex < playersList.length) {
          currentBid = playersList(currentPlayerIndex).basePrice
          highestBidder = "None"
        }
      }
    }

    if (k == 'r' || k == 'R') {
      myBudget = 5000
      cpuBudget = 5000
      mySquadCount = 0
      cpuSquadCount = 0
      currentPlayerIndex = 0
      mySquad.clear()
      cpuSquad.clear()
      generateRandomPlayers()
    }
  }
}

println("=== IPL AUCTION GAME LOADED SUCCESSFULLY ===")