24/02/2023, 17:36
Hi All,
I tried generating this codes from CHATGPT, but i am not sure if it is correct because I don't know how to create a tcl script.
The script is about family feud game, to set only individual players but originally this game is a team game.
I hope anyone can help me improve the script? If anyone interested...
Thank you.
Here's another code generated from CHATGPT with a team game
if you think this is worth to enhance the script, we'll give it a go..
Thank you everyone.
I tried generating this codes from CHATGPT, but i am not sure if it is correct because I don't know how to create a tcl script.
The script is about family feud game, to set only individual players but originally this game is a team game.
I hope anyone can help me improve the script? If anyone interested...
Thank you.
Code:
# Set the game variables
set question {Name something you find in a kitchen}
set answers {Plate Bowl Knife Spoon Fork Glass}
set num_answers [llength $answers]
set points {10 20 30 40 50}
set num_points [llength $points]
# Initialize the game
proc start_game {} {
global question answers num_answers points num_points
puts "Welcome to Family Feud!"
puts "The question is: $question"
puts "There are $num_answers possible answers worth $num_points points."
set shuffled_answers [shuffle $answers]
set current_answer 0
set current_point 0
}
# Shuffle the answers to make the game more interesting
proc shuffle {lst} {
set out {}
while {[llength $lst] > 0} {
set i [expr {int(rand()*[llength $lst])}]
lappend out [lindex $lst $i]
set lst [lreplace $lst $i $i]
}
return $out
}
# Get the next answer and point value
proc get_answer {} {
global shuffled_answers current_answer points current_point num_points
set answer [lindex $shuffled_answers $current_answer]
set point [lindex $points $current_point]
incr current_answer
if {$current_answer >= $num_answers} {
set shuffled_answers [shuffle $shuffled_answers]
set current_answer 0
}
incr current_point
if {$current_point >= $num_points} {
set current_point 0
}
return [list $answer $point]
}
# Handle the user's guess
proc guess {nick guess} {
global question
set guess [string tolower $guess]
set answer [lsearch -exact -inline -all $answers $guess]
if {[llength $answer] > 0} {
set [incr score_$nick [lindex [get_answer] 1]] 1
puts "$nick guessed \"$guess\" and it is on the board! (+[lindex [get_answer] 1] points)"
puts "Current score:"
foreach n [sort [array names score_*]] {
puts "$n: $score_($n)"
}
} else {
puts "$nick guessed \"$guess\" and it is not on the board. Try again!"
}
}
# Start the game when someone says !familyfeud
bind pub - !familyfeud start_game
# Handle guesses when someone says !guess
bind pub - !guess {guess $user [lindex $argv 0]}
# Reset the scores when someone says !reset
bind pub - !reset {foreach n [array names score_*] {unset $n}}
Here's another code generated from CHATGPT with a team game
Code:
# Set up the questions and answers
set questions {
"Name a fruit that is red"
"Name a country in Europe"
"Name a type of animal that can swim"
}
set answers {
{Apple|Cherry|Strawberry|Watermelon}
{France|Germany|Italy|Spain}
{Dolphin|Fish|Otter|Seal}
}
# Set up the game variables
set round 0
set questionIndex -1
set answerIndex -1
set team1Score 0
set team2Score 0
# Function to get the next question and answer
proc getNextQuestionAndAnswer {} {
global questions answers questionIndex answerIndex
incr questionIndex
set question [lindex $questions $questionIndex]
set answer [lindex $answers $questionIndex]
set answerIndex -1
return $question
}
# Function to get the next answer for the current question
proc getNextAnswer {} {
global answers questionIndex answerIndex
incr answerIndex
set answerList [split [lindex $answers $questionIndex] "|"]
set answer [lindex $answerList $answerIndex]
return $answer
}
# Function to display the current scores
proc displayScores {} {
global team1Score team2Score
puts "Team 1: $team1Score"
puts "Team 2: $team2Score"
}
# Function to handle a correct answer
proc handleCorrectAnswer {team} {
global team1Score team2Score
if {$team == 1} {
incr team1Score
} else {
incr team2Score
}
displayScores
}
# Function to handle a wrong answer
proc handleWrongAnswer {} {
global team1Score team2Score
displayScores
}
# Function to start a new round
proc startNewRound {} {
global round
set round [expr {$round + 1}]
puts "Round $round"
set question [getNextQuestionAndAnswer]
puts "Question: $question"
}
# Function to play the game
proc playGame {} {
global team1Score team2Score
while {1} {
set answer [getNextAnswer]
set response [gets stdin]
if {[string match -nocase $answer $response]} {
puts "Correct!"
handleCorrectAnswer [clock seconds % 2]
startNewRound
} elseif {$response eq ""} {
puts "Time's up!"
handleWrongAnswer
startNewRound
} else {
puts "Wrong!"
}
}
}
# Start the game
startNewRound
playGame
if you think this is worth to enhance the script, we'll give it a go..
Thank you everyone.