Code Sketch


yioiiii teacher ai
By: Mhalsakant School
Category: Programming
import javax.swing._
import java.awt._
import java.awt.event._
import java.io._

val secret =
  Array(121,97,100,110,101,115,104,50,48,49,51)
    .map(_.toChar)
    .mkString

var speechProcess: Process = null

def stopVoice(): Unit = {
  try {
    if (speechProcess != null) {
      speechProcess.destroy()
      speechProcess = null
    }
  } catch {
    case _: Throwable =>
      speechProcess = null
  }
}

def speak(text: String): Unit = {

  stopVoice()

  val clean =
    text
      .replace("\"", "'")
      .replace("\n", " ")
      .replace("\r", " ")
      .replace("&", " and ")

  val safe =
    clean
      .replace("'", "''")

  val command =
    "Add-Type -AssemblyName System.Speech; " +
    "$v = New-Object System.Speech.Synthesis.SpeechSynthesizer; " +
    "$v.Rate = -1; " +
    "$v.Volume = 100; " +
    "$v.Speak('" + safe + "');"

  try {
    speechProcess =
      new ProcessBuilder(
        "powershell.exe",
        "-NoProfile",
        "-ExecutionPolicy",
        "Bypass",
        "-Command",
        command
      ).start()
  } catch {
    case _: Throwable =>
      speechProcess = null
  }
}

def makeButton(text: String): JButton = {
  val b = new JButton(text)
  b.setFont(new Font("SansSerif", Font.BOLD, 14))
  b
}

def makeArea(text: String): JTextArea = {
  val a = new JTextArea()
  a.setText(text)
  a.setEditable(false)
  a.setLineWrap(true)
  a.setWrapStyleWord(true)
  a.setFont(new Font("SansSerif", Font.PLAIN, 18))
  a.setBorder(
    BorderFactory.createEmptyBorder(15,15,15,15)
  )
  a
}

def openTeacher(): Unit = {

  val frame =
    new JFrame("AI Teacher - Scala Swing")

  frame.setSize(1100,750)

  frame.setDefaultCloseOperation(
    WindowConstants.EXIT_ON_CLOSE
  )

  frame.setLocationRelativeTo(null)

  val root =
    new JPanel(new BorderLayout())

  root.setBackground(Color.WHITE)

  val header =
    new JPanel(new BorderLayout())

  header.setBackground(
    new Color(25,25,35)
  )

  val title =
    new JLabel(
      "AI TEACHER - SCALA SWING",
      SwingConstants.CENTER
    )

  title.setForeground(Color.WHITE)

  title.setFont(
    new Font("SansSerif",Font.BOLD,30)
  )

  val subtitle =
    new JLabel(
      "Listen ? Learn ? Practice ? Build",
      SwingConstants.CENTER
    )

  subtitle.setForeground(Color.LIGHT_GRAY)

  subtitle.setFont(
    new Font("SansSerif",Font.PLAIN,16)
  )

  header.add(
    title,
    BorderLayout.NORTH
  )

  header.add(
    subtitle,
    BorderLayout.SOUTH
  )

  root.add(
    header,
    BorderLayout.NORTH
  )

  val lessons =
    Array(
      Array(
        "STEP 1 - SCALA",
        "Namaskar! Aaj apan Scala Swing shiknar aahot. Scala hi programming language aahe. Swing cha use desktop graphical applications banvnyasathi karta yeto."
      ),
      Array(
        "STEP 2 - ENVIRONMENT",
        "Ata tumhi jya environment madhye Scala run karta te important aahe. ChatGPT la tumcha exact environment sangaa. Udaharanarth plain Scala, IntelliJ, kiwa Kojo."
      ),
      Array(
        "STEP 3 - NEW FILE",
        "Ata ek new Scala source file tayar kara. Jar tumcha environment single file support kart asel tar ChatGPT la single file code denyas ?????."
      ),
      Array(
        "STEP 4 - IDEA",
        "Ata project chi idea ????. Udaharanarth calculator, quiz, maze, racing game, drawing app kiwa learning application."
      ),
      Array(
        "STEP 5 - REQUIREMENTS",
        "Project madhye kay pahije te list kara. Player, controls, score, levels, buttons, screens, graphics, win condition ani game over condition."
      ),
      Array(
        "STEP 6 - CHATGPT",
        "ChatGPT la ?????: Tu maza expert Scala Swing developer ahes. Mala complete runnable code dya. Complete imports dya. Undefined variables nako. Missing methods nako."
      ),
      Array(
        "STEP 7 - COPY",
        "ChatGPT ne code dila ki complete code select kara ani Copy kara. Partial code copy karu naka."
      ),
      Array(
        "STEP 8 - PASTE",
        "Ata Scala editor madhla old code select kara. Old code replace kara ani ChatGPT kadun milalela complete code paste kara."
      ),
      Array(
        "STEP 9 - SAVE",
        "Code paste kelyanantar Save kara. Save zalyanantar Run button press kara."
      ),
      Array(
        "STEP 10 - RUN",
        "Run kelyavar GUI window disli tar basic program working aahe. Window disat nasel tar startup ani setVisible check karayla ChatGPT la ?????."
      ),
      Array(
        "STEP 11 - ERROR",
        "Compiler error ala tar ????? ???. Complete error message copy kara. Fakat ek line nahi, complete compiler output dya."
      ),
      Array(
        "STEP 12 - ERROR FIX",
        "ChatGPT la complete error, current code ani expected behavior dya. Nantar ?????: Fakta ek line fix karu naka. Complete corrected replacement code dya."
      ),
      Array(
        "STEP 13 - JFRAME",
        "JFrame mhanje main window. Tyachya madhye JPanel, JLabel, JButton ani itar components add karta yetat."
      ),
      Array(
        "STEP 14 - BUTTON",
        "JButton ha clickable button aahe. Button click ???????? ActionListener cha use ???? action execute karta yeto."
      ),
      Array(
        "STEP 15 - MOUSE",
        "Mouse control sathi mouse events vaparta yetat. Mouse position madhun X ani Y coordinates miltat."
      ),
      Array(
        "STEP 16 - KEYBOARD",
        "Keyboard game sathi arrow keys kiwa W A S D vaparta yetat. Key press mule player position change karta yeti."
      ),
      Array(
        "STEP 17 - TIMER",
        "Game madhye repeated update sathi Swing Timer useful aahe. Timer update karto ani repaint mule screen ?????? draw karta yete."
      ),
      Array(
        "STEP 18 - DRAWING",
        "Graphics object madhun rectangle, circle, line ani text draw karta yetat. Custom game graphics sathi paintComponent important aahe."
      ),
      Array(
        "STEP 19 - COLLISION",
        "Player obstacle la touch karto ka he collision checking ne samajta. Coin collect, enemy hit kiwa finish line sathi collision useful aahe."
      ),
      Array(
        "STEP 20 - SCORE",
        "Coins collect kelyavar score vadhvu shakto. Level complete zalyavar bonus deu shakto. Score screen var continuously display kara."
      ),
      Array(
        "STEP 21 - LEVELS",
        "Multiple levels madhye gradually difficulty ?????. Enemy speed, obstacle count ani layout change karta yeto."
      ),
      Array(
        "STEP 22 - SCREENS",
        "Professional project madhye Main Menu, Instructions, Game, Pause, Game Over, Win ani Settings screens add karta yetat."
      ),
      Array(
        "STEP 23 - RESTART",
        "Restart ?????? player position, score, level, speed ani game state ????? values ?? reset kara."
      ),
      Array(
        "STEP 24 - UPGRADE",
        "Project working ???????? feature by feature upgrade kara. ChatGPT la ?????: Existing features remove karu naka. New feature integrate kara. Complete updated code dya."
      ),
      Array(
        "STEP 25 - EXPERT",
        "Expert honyasathi ????? idea design kara, requirements ????, ChatGPT prompt ???? ???, code run kara, errors fix kara ani ?????? upgrade kara."
      )
    )

  var current =
    0

  val stepTitle =
    new JLabel(
      lessons(0)(0),
      SwingConstants.CENTER
    )

  stepTitle.setFont(
    new Font("SansSerif",Font.BOLD,25)
  )

  stepTitle.setBorder(
    BorderFactory.createEmptyBorder(
      20,10,10,10
    )
  )

  val stepArea =
    makeArea(
      lessons(0)(1)
    )

  stepArea.setBackground(Color.WHITE)

  val progress =
    new JLabel(
      "Lesson 1 / " + lessons.length,
      SwingConstants.CENTER
    )

  progress.setFont(
    new Font("SansSerif",Font.BOLD,15)
  )

  val center =
    new JPanel(new BorderLayout())

  center.setBackground(Color.WHITE)

  center.add(
    stepTitle,
    BorderLayout.NORTH
  )

  center.add(
    new JScrollPane(stepArea),
    BorderLayout.CENTER
  )

  center.add(
    progress,
    BorderLayout.SOUTH
  )

  root.add(
    center,
    BorderLayout.CENTER
  )

  val controls =
    new JPanel(new FlowLayout())

  controls.setBackground(Color.WHITE)

  val previous =
    makeButton("<< PREVIOUS")

  val play =
    makeButton("? PLAY")

  val stop =
    makeButton("? STOP")

  val next =
    makeButton("NEXT >>")

  val replay =
    makeButton("? REPLAY")

  val teacher =
    makeButton("? TEACH")

  controls.add(previous)
  controls.add(play)
  controls.add(stop)
  controls.add(next)
  controls.add(replay)
  controls.add(teacher)

  root.add(
    controls,
    BorderLayout.SOUTH
  )

  def updateLesson(): Unit = {

    stepTitle.setText(
      lessons(current)(0)
    )

    stepArea.setText(
      lessons(current)(1)
    )

    progress.setText(
      "Lesson " +
      (current + 1) +
      " / " +
      lessons.length
    )
  }

  def playLesson(): Unit = {

    val text =
      lessons(current)(0) +
      ". " +
      lessons(current)(1)

    val thread =
      new Thread(
        new Runnable {
          override def run(): Unit = {
            speak(text)
          }
        }
      )

    thread.start()
  }

  previous.addActionListener(
    new ActionListener {
      override def actionPerformed(
          e: ActionEvent
      ): Unit = {

        stopVoice()

        if (current > 0) {
          current -= 1
          updateLesson()
        }
      }
    }
  )

  next.addActionListener(
    new ActionListener {
      override def actionPerformed(
          e: ActionEvent
      ): Unit = {

        stopVoice()

        if (current < lessons.length - 1) {
          current += 1
          updateLesson()
        }
      }
    }
  )

  play.addActionListener(
    new ActionListener {
      override def actionPerformed(
          e: ActionEvent
      ): Unit = {
        playLesson()
      }
    }
  )

  teacher.addActionListener(
    new ActionListener {
      override def actionPerformed(
          e: ActionEvent
      ): Unit = {
        playLesson()
      }
    }
  )

  replay.addActionListener(
    new ActionListener {
      override def actionPerformed(
          e: ActionEvent
      ): Unit = {
        playLesson()
      }
    }
  )

  stop.addActionListener(
    new ActionListener {
      override def actionPerformed(
          e: ActionEvent
      ): Unit = {
        stopVoice()
      }
    }
  )

  frame.setContentPane(root)
  frame.setVisible(true)
}

val login =
  new JFrame("Private AI Teacher")

login.setSize(450,280)

login.setDefaultCloseOperation(
  WindowConstants.EXIT_ON_CLOSE
)

login.setLocationRelativeTo(null)

val loginPanel =
  new JPanel(new GridBagLayout())

loginPanel.setBackground(Color.WHITE)

val g =
  new GridBagConstraints()

g.insets =
  new Insets(9,9,9,9)

g.fill =
  GridBagConstraints.HORIZONTAL

val heading =
  new JLabel(
    "PRIVATE AI TEACHER",
    SwingConstants.CENTER
  )

heading.setFont(
  new Font("SansSerif",Font.BOLD,27)
)

val message =
  new JLabel(
    "Enter access code",
    SwingConstants.CENTER
  )

val password =
  new JPasswordField()

password.setPreferredSize(
  new Dimension(280,36)
)

val unlock =
  makeButton("UNLOCK")

val status =
  new JLabel(
    " ",
    SwingConstants.CENTER
  )

g.gridx = 0
g.gridy = 0
g.gridwidth = 2

loginPanel.add(
  heading,
  g
)

g.gridy = 1

loginPanel.add(
  message,
  g
)

g.gridy = 2

loginPanel.add(
  password,
  g
)

g.gridy = 3
g.gridwidth = 1
g.gridx = 0

loginPanel.add(
  unlock,
  g
)

g.gridx = 1

loginPanel.add(
  status,
  g
)

login.setContentPane(
  loginPanel
)

def check(): Unit = {

  val entered =
    new String(
      password.getPassword
    )

  if (entered == secret) {

    login.dispose()

    openTeacher()

  } else {

    status.setText(
      "ACCESS DENIED"
    )

    password.setText("")
  }
}

unlock.addActionListener(
  new ActionListener {
    override def actionPerformed(
        e: ActionEvent
    ): Unit = {
      check()
    }
  }
)

password.addActionListener(
  new ActionListener {
    override def actionPerformed(
        e: ActionEvent
    ): Unit = {
      check()
    }
  }
)

login.setVisible(true)

password.requestFocusInWindow()