Ruby Following Assignment Taken Book Jerry Lee Ford Jr 1th Edition Ruby Programming Pg 364 Q43876413




////////////////////////////////////// THE CODE:///////////////////////////////////////














FOR RUBY The following assignment was taken from the book: Jerry Lee Ford, Jr., 1th edition, Ruby Programming, pg. 364 Please solve this assignment. Thanks. Reinforcement Exercises The following exercises are designed to further your understanding of Ruby programming by challenging you to make improvements to the chapter’s game project, the Ruby Tic-Tac-Toe game. 1. Currently, any time a player makes an invalid move, the game rejects it without an explanation. Player input may be invalid for several different reasons, however. A player may attempt to choose a game-board square that has already been assigned, or a player may submit invalid game-board coordinates or the wrong number of characters. Modify the game to display text messages that identify when an invalid move has been made and explain why that move was invalid. To implement this change, add a method to the Game class. Name the method display_error and configure it to accept a single argument, a text string containing the error message …to be displayed. Modify the display_game_board method so that it calls upon the display_error method whenever invalid input is received from a player. Make sure that calls to the display_error method contain suitable error messages in the form of a text string. 2. The game allows players to play as many times as they wish. However, it is currently up to players themselves to keep track of the number of games they win. Relieve players of this burden by having the game keep track of the number of games each player wins. Rather than displaying this statistical information after each game, make it available to the players as a hidden cheat. And make the statistics accessible during game play, allowing players to access this data by typing the letter h or H in place of a move during game play. To implement this change, add two variables named SxWins and SoWins. Add the definition statement for these two variables at the beginning of the program’s Main Script Logic section, and assign both variables a starting value of O. Add a new method named display_statistics to the Game class. Configure the method so that it converts the numeric value stored in the two variables to a string and then displays its values so that players can see it. Next, add two statements to the play-game method that increment the value of $xWins and SoWins each time Player X or Player O wins a game. Lastly, modify the display_game_board method so that it executes the display_statistics method whenever a player enters H and presses Enter. 3. Currently, the game requires Player X to go first, putting Player O at a disadvantage. To make things fairer, modify the Main Script Logic section so that Player X begins the first round of play, but after that the players take turns beginning a round of play. In addition, modify the call to the play game method so that it includes the passage of the player variable. Provide that method with instruction (e.g., a text string of X or O) on whose turn it is. Then modify the play_game method to accept a text string as a parameter representing the player whose turn it is. Lastly, remove the hardcode statement at the beginning of the play_game method, which makes Player X begin every round of play. 4. Currently, the game’s welcome screen instructs players to press Enter to continue. No option is provided for immediate termination of the game should the players decide not to play. Instead, the players have to press Enter and then respond to a different prompt to terminate game play. Remove this unnec- essary step To implement this change, modify the Game class’s display_greeting method so that it prompts the play- ers to either type qor Q and press Enter to terminate game play or press Enter to continue playing. Collect this input using the STDIN class’s gets method, and then use the chop! method to remove the end-of-line marker from the input. Add a statement at the end of the method that returns the player’s response to the statement that called on the method for execution. Next, modify the statement at the begin- ning of the Main Script Logic section that executes the display_greeting method so that it captures the result now returned by that method. Lastly, add programming logic that either terminates the game or allows game play to continue, based on the value returned from the display_greeting method. 5. Currently, the game assumes that both players already know how to play the game. However, this may not always be the case. To make the game more user friendly, add an option that allows players to view Wikipedia’s Tic-Tac-Toe page. To implement this change, modify the Game class’s display_greeting method so that it presents players with the option of typing mor Mand pressing Enter to load the Wikipedia page into their default Web browser. Further, modify the display_greeting method so that it executes the following statement whenever a player elects to view more information about Tic-Tac-Toe: system(‘start http://en.wikipedia.org/wiki/7 Tic tac_toe’) 1 # Define custom classes — Ovo 3 #Define a class representing the console window 4 v class Screen 5 v def cls #Define a method that clears the display area puts (“n” * 25) #Scroll the screen 25 times puts “la” #Make a little noise to get the player’s #attention end O o o AB WEB def pause #Define a method that pauses the display #area STDIN.gets #Execute the STDIN class’s gets method to #pause script execution until the player #presses the Enter key end end #Define a class representing the Ruby Tic-Tac-Toe game 23 v class Game #This method displays the game’s opening message def display_greeting Console_Screen.cls #clear the display area #Display the game’s welcome screen puts “tttWelcome to the Ruby Tic-Tac-Toe Game!” “nnnn” nuts. “ttt puts “tttWelcome to the Ruby Tic-Tac-Toe Game!” + “nnnn” puts “ttt puts “ttt X 11” puts “ttt puts “ttt puts “ttt puts “ttt OLX 0” puts “ttt puts “ttt puts “ttt puts “ttt TX puts “ttt print “nnnnnPress Enter to continue.” Console_Screen.pause #Pause the game end #Define a method to display the game board and collect #player moves def display_game_board(player) move = “” #Assign a default value loop do #Loop forever Console_Screen.cls #clear the display area #Display the game board puts “tttWelcome to the Ruby Tic-Tac-Toe Game! ” + “nnnn” nuts. “ttt 3n” 2 3 n” A #{$A1} | #{$A2} | #{$A3}”, —————– “nnnn” puts “ttt puts “ttt puts “ttt puts “ttt puts “ttt puts “ttt puts “ttt puts “ttt puts “ttt puts “ttt puts “ttt puts “ttt B #{$B1} #{$B2) | #{$B3}” C #{$C1} #{$C2) | #{$C3}”, #Prompt the player to enter a move print “nnnnnPlayer ” + player + “‘s turn. ” + “Please enter your move: “. move = STDIN.gets #Collect the player’s move move.chop! #Remove the end of line marker move = move.upcase #Convert to uppercase #Terminate the loop if a valid move was entered if move. length == 2 then #Must be at 2 character long if move = /[A-C][1-3]/i #Must be A1, A2, A3, B1, #B2, B3, C1, C2, or c3 #Call method responsible for determining if the #board square was available validMove – validate_player_move (move), if validMove == true #The move was valid break #Terminate the execution of the loop end if validMove == true #The move was valid break #Terminate the execution of the loop end end end end return move #Return the player’s move back to the #calling statement end 98 99 100 101 102 103 104 105 106 107 108 109 110 111 #Define a method that determines if the square selected by #the player is still available def validate_player_move (move) 112 113 114 115 116 117 118 119 120 121 122 #Return a value of false is the square has already been #selected return false if move == “A1” && $A1 != return false if move == “81” && $B1 return false if move == “01” && $C1 return false if move == “A2” && $A2 != “” return false if move == “B2” && $B2 return false if move == “C2” && $C2 != “” return false if move == “A3” && $A3 return false if move == “B3” && $B3 != return false if move == “C3” && $C3 != “” #Return a value of true if the square is available return true 122 end 123 end 124 126 #Define a method to be used to display game instructions def display_instructions 127 128 129 130 Console_Screen.cls #clear the display area puts “INSTRUCTIONS:nn” #Display a heading, 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 #Display the game’s instructions puts “In the Ruby Tic-Tac-Toe game, Player X and ” + “Player o try to ” puts “outmaneuver each other by placing three Xs or ” + “Os in a row” puts “horizontally, vertically, or diagonally. ” + “Player x goes first.” puts “The game ends when either player claims three ” + “squares in a ” puts “row or when all nine squares have been claimed ” + “without either” puts “player managing to claim three squares in a row.” puts “nnnnnnnnnnn” print “Press Enter to continue.” Console_Screen.pause #Pause the game end #Define a method to control game play def play_game end 149 150 151 152 153 #Define a method to control game play def play_game 154 155 player = “X” #Make Player x the default player for each #new game 156 157 158 159 noOfMoves = 0 #Reset the value of the variable used to #keep track of the total number of moves #made in a game 160 161 162 #clear out the game board to get it ready for a new game clear_game_board 163 loop do #Loop forever Console_Screen.cls #Clear the display area 164 165 166 167 168 169 170 #Call on the method that displays the game board and #collects player moves square = display_game_board(player) 172 173 174 175 176 177 178 179 180 101 #Assign the selected game board square to the player #that selected it $A1 = player if square == “A1” $A2 = player if square == “A2” $A3 = player if square == “A3” $B1 = player if square == “81” $B2 = player if square == $B3 = player if square == “B3” $C1 = player if square == “01” donlauan 179 180 181 182 183 184 $B3 = player if square == “B3” $C1 = player if square == “C1” $C2 = player if square == “C2” $C3 = player if square == “C3” 185 #Keep count of the total number of moves that have #been made noOfMoves += 1 #Call on the method that is responsible for #determining if the game has been won winner = check_results(player) 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 #See is player X has won if winner == “X” then #Call on the method that displays the game final #results display_game_results(“Player X Wins!”), break #Terminate the execution of the loop end #See if player o has won if winner == “0” then #Call on the method that displays the game final #results display_game_results(“Player O Wins!”), break #Terminate the execution of the loop end #See if the game has ended in a tie if noOfMoves == 9 then #Call on the method that displays the game final We were unable to transcribe this imagePCI- $C2 = $C3 = — — 240 241 242 243 244 245 246 end #Define a method to examine the game board and determin #if the current player has won the game def check_results(player) 248 249 250 winner = “” #Assign a default value 251 252 v 253 254 v 255 256 v #Check vertically winner = player if $A1 == player && $A2 == player && $A3 == player winner = player if $B1 == player && $B2 == player && $B3 == player winner – player if $C1 == player && $C2 == player && $C3 == player 257 258 259 #Check horizontally winner = player if $41 == player && $B1 == player && $C1 == player winner = player if $A2 == player && $B2 == player && $C2 == player winner = player if $A3 == player && $B3 == player && $C3 == player 260 V 261 262 v 263 264 v 265 266 267 268 V 269 270 v 271 #check diagonally winner = player if $A1 == player && $B2 == player && $C3 == player winner = player if $A3 == player && $B2 == player && $C1 == player winner = player if $A3 == player && $B2 == player && $C1 == player return winner #Return the result back to the calling #statement end 270 v 271 272 273 v 274 275 276 277 278 279 280 v 281 282 283 284 285 286 287 288 #Define a method that will be used to displays the game’s #final result def display_game_results(message) Console_Screen.cls #clear the display area 289 ——- 290 291 292 293 294 #Display the results of the game puts “nnn” puts “ttt Game Over: ” + message + “nnn”, puts “ttt 1 2 3 n”, puts “ttt puts “ttt A #{$A1} | #{$A2} | #{$A3)” puts “ttt puts “ttt puts “ttt puts “ttt B #{$B1 #{$B2) 1 # $B3}” puts “ttt puts “ttt puts “ttt puts “ttt C #{$C1} | #{$C2} #{$C3}”, puts “ttt print “nnnnnPress Enter to continue. ” 295 296 297 298 299 300 301 302 Console_Screen.pause #Pause the game 301 Console_Screen.pause #Pause the game 302 303 304 end 305 #This method displays information about the Ruby #Tic-Tac-Toe game def display_credits 306 307 v 308 309 Console_Screen.cls #clear the display area ليبيا ليبيا ليبيا ليبيا 314 بيا بيا بيا #Thank the player and display game information puts “tt Thank you for playing the Ruby ” + “Tic-Tac-Toe game. nnnn” puts “nttt Developed by Jerry Lee Ford, Jr.nn”, puts “tttt Copyright 2010nn” puts “tttURL: http://www.tech-publishing.comnn” + “nnnnnnnn” 317 318 end mmm end 322 323 324 # Main Script Logic — 325 326 V Console_Screen = Screen.new #Instantiate a new Screen 327 #object 328 TIT = Game.new #Instantiate a new Game object 329 #Execute the Game class’s display_greeting method 331 TTT.display_greeting 332 222 #Evecuto the Game classic clean name hoard method 331 TIT.display_greeting 332 333 #Execute the Game class’s clear_game_board method TIL.clear_game_board 334 335 336 answer = “” #Initialize variable and assign it an empty #string 337 338 339 340 341 342 #Loop until the player enters y or n and do not accept any #other input loop do Console_Screen.cls #clear the display area #Prompt the player for permission to start the game print “Are you ready to play Ruby Tic-Tac-Toe? (y/n): ” answer = STDIN.gets #Collect the player’s answer answer.chop! #Remove the end of line marker 351 #Terminate the loop if valid input was provided break if answer =/yn/i 352 354 end 355 356 357 358 359 #Analyze the player’s answer if answer == “n” #See if the player want to quit Console_Screen.cls #clear the display area 360 #Invite the player to return and play the game some other #time جه در جام anbone anothan mo 361 362 #Invite the player to return and play the game some other #time puts “Okay, perhaps another time. nn” else #The player wants to play the game 366 367 #Execute the Game class’s display_instructions method TTT.display_instructions playAgain = “” #Initialize variable and assign it an #empty string 373 loop do #Loop forever 375 376 #Execute the Game class’s play game method TIT.play_game 377 378 loop do #Loop forever Console_Screen.cls #clear the display area #Find out if the player wants to play another round print “Would you like to play another round? (y/n): ” playAgain = STDIN.gets #Collect the player’s #response playAgain.chop! #Remove the end of line marker #Terminate the loop if valid input was provided break if playAgain =r /n[y/i end 392 the loan 1 Volid; end #Terminate the loop if valid input was provided break if play Again an /n/i 390 391 392 393 394 395 396 397 398 399 400 401 402 end #Call upon the Game class’s determine_credits method TIT.display_credits end Show transcribed image text FOR RUBY The following assignment was taken from the book: Jerry Lee Ford, Jr., 1th edition, Ruby Programming, pg. 364 Please solve this assignment. Thanks. Reinforcement Exercises The following exercises are designed to further your understanding of Ruby programming by challenging you to make improvements to the chapter’s game project, the Ruby Tic-Tac-Toe game. 1. Currently, any time a player makes an invalid move, the game rejects it without an explanation. Player input may be invalid for several different reasons, however. A player may attempt to choose a game-board square that has already been assigned, or a player may submit invalid game-board coordinates or the wrong number of characters. Modify the game to display text messages that identify when an invalid move has been made and explain why that move was invalid. To implement this change, add a method to the Game class. Name the method display_error and configure it to accept a single argument, a text string containing the error message …to be displayed. Modify the display_game_board method so that it calls upon the display_error method whenever invalid input is received from a player. Make sure that calls to the display_error method contain suitable error messages in the form of a text string.
2. The game allows players to play as many times as they wish. However, it is currently up to players themselves to keep track of the number of games they win. Relieve players of this burden by having the game keep track of the number of games each player wins. Rather than displaying this statistical information after each game, make it available to the players as a hidden cheat. And make the statistics accessible during game play, allowing players to access this data by typing the letter h or H in place of a move during game play. To implement this change, add two variables named SxWins and SoWins. Add the definition statement for these two variables at the beginning of the program’s Main Script Logic section, and assign both variables a starting value of O. Add a new method named display_statistics to the Game class. Configure the method so that it converts the numeric value stored in the two variables to a string and then displays its values so that players can see it. Next, add two statements to the play-game method that increment the value of $xWins and SoWins each time Player X or Player O wins a game. Lastly, modify the display_game_board method so that it executes the display_statistics method whenever a player enters H and presses Enter. 3. Currently, the game requires Player X to go first, putting Player O at a disadvantage. To make things fairer, modify the Main Script Logic section so that Player X begins the first round of play, but after that the players take turns beginning a round of play. In addition, modify the call to the play game method so that it includes the passage of the player variable. Provide that method with instruction (e.g., a text string of X or O) on whose turn it is. Then modify the play_game method to accept a text string as a parameter representing the player whose turn it is. Lastly, remove the hardcode statement at the beginning of the play_game method, which makes Player X begin every round of play.
4. Currently, the game’s welcome screen instructs players to press Enter to continue. No option is provided for immediate termination of the game should the players decide not to play. Instead, the players have to press Enter and then respond to a different prompt to terminate game play. Remove this unnec- essary step To implement this change, modify the Game class’s display_greeting method so that it prompts the play- ers to either type qor Q and press Enter to terminate game play or press Enter to continue playing. Collect this input using the STDIN class’s gets method, and then use the chop! method to remove the end-of-line marker from the input. Add a statement at the end of the method that returns the player’s response to the statement that called on the method for execution. Next, modify the statement at the begin- ning of the Main Script Logic section that executes the display_greeting method so that it captures the result now returned by that method. Lastly, add programming logic that either terminates the game or allows game play to continue, based on the value returned from the display_greeting method.
5. Currently, the game assumes that both players already know how to play the game. However, this may not always be the case. To make the game more user friendly, add an option that allows players to view Wikipedia’s Tic-Tac-Toe page. To implement this change, modify the Game class’s display_greeting method so that it presents players with the option of typing mor Mand pressing Enter to load the Wikipedia page into their default Web browser. Further, modify the display_greeting method so that it executes the following statement whenever a player elects to view more information about Tic-Tac-Toe: system(‘start http://en.wikipedia.org/wiki/7 Tic tac_toe’)
1 # Define custom classes — Ovo 3 #Define a class representing the console window 4 v class Screen 5 v def cls #Define a method that clears the display area puts (“n” * 25) #Scroll the screen 25 times puts “la” #Make a little noise to get the player’s #attention end O o o AB WEB def pause #Define a method that pauses the display #area STDIN.gets #Execute the STDIN class’s gets method to #pause script execution until the player #presses the Enter key end end #Define a class representing the Ruby Tic-Tac-Toe game 23 v class Game #This method displays the game’s opening message def display_greeting Console_Screen.cls #clear the display area #Display the game’s welcome screen puts “tttWelcome to the Ruby Tic-Tac-Toe Game!” “nnnn” nuts. “ttt
puts “tttWelcome to the Ruby Tic-Tac-Toe Game!” + “nnnn” puts “ttt puts “ttt X 11” puts “ttt puts “ttt puts “ttt puts “ttt OLX 0” puts “ttt puts “ttt puts “ttt puts “ttt TX puts “ttt print “nnnnnPress Enter to continue.” Console_Screen.pause #Pause the game end #Define a method to display the game board and collect #player moves def display_game_board(player) move = “” #Assign a default value loop do #Loop forever Console_Screen.cls #clear the display area #Display the game board puts “tttWelcome to the Ruby Tic-Tac-Toe Game! ” + “nnnn” nuts. “ttt 3n”
2 3 n” A #{$A1} | #{$A2} | #{$A3}”, —————– “nnnn” puts “ttt puts “ttt puts “ttt puts “ttt puts “ttt puts “ttt puts “ttt puts “ttt puts “ttt puts “ttt puts “ttt puts “ttt B #{$B1} #{$B2) | #{$B3}” C #{$C1} #{$C2) | #{$C3}”, #Prompt the player to enter a move print “nnnnnPlayer ” + player + “‘s turn. ” + “Please enter your move: “. move = STDIN.gets #Collect the player’s move move.chop! #Remove the end of line marker move = move.upcase #Convert to uppercase #Terminate the loop if a valid move was entered if move. length == 2 then #Must be at 2 character long if move = /[A-C][1-3]/i #Must be A1, A2, A3, B1, #B2, B3, C1, C2, or c3 #Call method responsible for determining if the #board square was available validMove – validate_player_move (move), if validMove == true #The move was valid break #Terminate the execution of the loop end
if validMove == true #The move was valid break #Terminate the execution of the loop end end end end return move #Return the player’s move back to the #calling statement end 98 99 100 101 102 103 104 105 106 107 108 109 110 111 #Define a method that determines if the square selected by #the player is still available def validate_player_move (move) 112 113 114 115 116 117 118 119 120 121 122 #Return a value of false is the square has already been #selected return false if move == “A1” && $A1 != return false if move == “81” && $B1 return false if move == “01” && $C1 return false if move == “A2” && $A2 != “” return false if move == “B2” && $B2 return false if move == “C2” && $C2 != “” return false if move == “A3” && $A3 return false if move == “B3” && $B3 != return false if move == “C3” && $C3 != “” #Return a value of true if the square is available return true 122 end
123 end 124 126 #Define a method to be used to display game instructions def display_instructions 127 128 129 130 Console_Screen.cls #clear the display area puts “INSTRUCTIONS:nn” #Display a heading, 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 #Display the game’s instructions puts “In the Ruby Tic-Tac-Toe game, Player X and ” + “Player o try to ” puts “outmaneuver each other by placing three Xs or ” + “Os in a row” puts “horizontally, vertically, or diagonally. ” + “Player x goes first.” puts “The game ends when either player claims three ” + “squares in a ” puts “row or when all nine squares have been claimed ” + “without either” puts “player managing to claim three squares in a row.” puts “nnnnnnnnnnn” print “Press Enter to continue.” Console_Screen.pause #Pause the game end #Define a method to control game play def play_game
end 149 150 151 152 153 #Define a method to control game play def play_game 154 155 player = “X” #Make Player x the default player for each #new game 156 157 158 159 noOfMoves = 0 #Reset the value of the variable used to #keep track of the total number of moves #made in a game 160 161 162 #clear out the game board to get it ready for a new game clear_game_board 163 loop do #Loop forever Console_Screen.cls #Clear the display area 164 165 166 167 168 169 170 #Call on the method that displays the game board and #collects player moves square = display_game_board(player) 172 173 174 175 176 177 178 179 180 101 #Assign the selected game board square to the player #that selected it $A1 = player if square == “A1” $A2 = player if square == “A2” $A3 = player if square == “A3” $B1 = player if square == “81” $B2 = player if square == $B3 = player if square == “B3” $C1 = player if square == “01” donlauan
179 180 181 182 183 184 $B3 = player if square == “B3” $C1 = player if square == “C1” $C2 = player if square == “C2” $C3 = player if square == “C3” 185 #Keep count of the total number of moves that have #been made noOfMoves += 1 #Call on the method that is responsible for #determining if the game has been won winner = check_results(player) 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 #See is player X has won if winner == “X” then #Call on the method that displays the game final #results display_game_results(“Player X Wins!”), break #Terminate the execution of the loop end #See if player o has won if winner == “0” then #Call on the method that displays the game final #results display_game_results(“Player O Wins!”), break #Terminate the execution of the loop end #See if the game has ended in a tie if noOfMoves == 9 then #Call on the method that displays the game final
PCI- $C2 = $C3 = — — 240 241 242 243 244 245 246 end #Define a method to examine the game board and determin #if the current player has won the game def check_results(player) 248 249 250 winner = “” #Assign a default value 251 252 v 253 254 v 255 256 v #Check vertically winner = player if $A1 == player && $A2 == player && $A3 == player winner = player if $B1 == player && $B2 == player && $B3 == player winner – player if $C1 == player && $C2 == player && $C3 == player 257 258 259 #Check horizontally winner = player if $41 == player && $B1 == player && $C1 == player winner = player if $A2 == player && $B2 == player && $C2 == player winner = player if $A3 == player && $B3 == player && $C3 == player 260 V 261 262 v 263 264 v 265 266 267 268 V 269 270 v 271 #check diagonally winner = player if $A1 == player && $B2 == player && $C3 == player winner = player if $A3 == player && $B2 == player && $C1 == player
winner = player if $A3 == player && $B2 == player && $C1 == player return winner #Return the result back to the calling #statement end 270 v 271 272 273 v 274 275 276 277 278 279 280 v 281 282 283 284 285 286 287 288 #Define a method that will be used to displays the game’s #final result def display_game_results(message) Console_Screen.cls #clear the display area 289 ——- 290 291 292 293 294 #Display the results of the game puts “nnn” puts “ttt Game Over: ” + message + “nnn”, puts “ttt 1 2 3 n”, puts “ttt puts “ttt A #{$A1} | #{$A2} | #{$A3)” puts “ttt puts “ttt puts “ttt puts “ttt B #{$B1 #{$B2) 1 # $B3}” puts “ttt puts “ttt puts “ttt puts “ttt C #{$C1} | #{$C2} #{$C3}”, puts “ttt print “nnnnnPress Enter to continue. ” 295 296 297 298 299 300 301 302 Console_Screen.pause #Pause the game
301 Console_Screen.pause #Pause the game 302 303 304 end 305 #This method displays information about the Ruby #Tic-Tac-Toe game def display_credits 306 307 v 308 309 Console_Screen.cls #clear the display area ليبيا ليبيا ليبيا ليبيا 314 بيا بيا بيا #Thank the player and display game information puts “tt Thank you for playing the Ruby ” + “Tic-Tac-Toe game. nnnn” puts “nttt Developed by Jerry Lee Ford, Jr.nn”, puts “tttt Copyright 2010nn” puts “tttURL: http://www.tech-publishing.comnn” + “nnnnnnnn” 317 318 end mmm end 322 323 324 # Main Script Logic — 325 326 V Console_Screen = Screen.new #Instantiate a new Screen 327 #object 328 TIT = Game.new #Instantiate a new Game object 329 #Execute the Game class’s display_greeting method 331 TTT.display_greeting 332 222 #Evecuto the Game classic clean name hoard method
331 TIT.display_greeting 332 333 #Execute the Game class’s clear_game_board method TIL.clear_game_board 334 335 336 answer = “” #Initialize variable and assign it an empty #string 337 338 339 340 341 342 #Loop until the player enters y or n and do not accept any #other input loop do Console_Screen.cls #clear the display area #Prompt the player for permission to start the game print “Are you ready to play Ruby Tic-Tac-Toe? (y/n): ” answer = STDIN.gets #Collect the player’s answer answer.chop! #Remove the end of line marker 351 #Terminate the loop if valid input was provided break if answer =/yn/i 352 354 end 355 356 357 358 359 #Analyze the player’s answer if answer == “n” #See if the player want to quit Console_Screen.cls #clear the display area 360 #Invite the player to return and play the game some other #time جه در جام anbone anothan mo
361 362 #Invite the player to return and play the game some other #time puts “Okay, perhaps another time. nn” else #The player wants to play the game 366 367 #Execute the Game class’s display_instructions method TTT.display_instructions playAgain = “” #Initialize variable and assign it an #empty string 373 loop do #Loop forever 375 376 #Execute the Game class’s play game method TIT.play_game 377 378 loop do #Loop forever Console_Screen.cls #clear the display area #Find out if the player wants to play another round print “Would you like to play another round? (y/n): ” playAgain = STDIN.gets #Collect the player’s #response playAgain.chop! #Remove the end of line marker #Terminate the loop if valid input was provided break if playAgain =r /n[y/i end 392 the loan 1 Volid;
end #Terminate the loop if valid input was provided break if play Again an /n/i 390 391 392 393 394 395 396 397 398 399 400 401 402 end #Call upon the Game class’s determine_credits method TIT.display_credits end
Expert Answer
Answer to FOR RUBY The following assignment was taken from the book: Jerry Lee Ford, Jr., 1th edition, Ruby Programming, pg. 364 P…
OR