Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

There are two players You may assign them names or just call them Player 1 and P

ID: 3709809 • Letter: T

Question

There are two players You may assign them names or just call them Player 1 and Player 2 When it is Player One's turn-you will randomly assign a point value for each letter that he guesses correctly. It will continue to be his turn until he guesses a letter that is not in the word or phrase. (don't change the point value until they guess a letter that is not in the word.) Then it is Player Two's turn. This player will continue with the word/phrase that the last Player started with and can continue guessing letters until there are none to guess or the player asked for a letter that is not in the phrase. If a player guesses a letter that is not there-the point value for the next player's guess will be altered with a random number. The total that a player scored will continue to be accumulated Once the entire word is completed-the person with the highest number of points wins. You may approach this in different ways. You are not constrained to only doing it one way- but must have all the requirements met

Explanation / Answer

CODE

#main.rb code

class Player
    attr_accessor :playername, :points # Creates getter and setter methods.


   def initialize(name, p)
      @playername = name
      @points = p
    
   end

end

def display_points(player1, player2)
  
    print " " << player1.playername << "=" << player1.points.to_s
  
    print " " << player2.playername << "=" << player2.points.to_s
  
end

def display_text(text)
    print " TEXT IS ["
    text.each do |st|
        print " " << st
    end
    print " ]"
  
end

def gettextcount(gametext)
    total = 0
    gametext.each do |st|
        total = total + st.length
    end
    print " There are total " << total.to_s << " characters "

    return total
end

  
def isgameover(played_text)
  
    status = true
  
    played_text.each do |st|
        st.each_char do |currentcharread|
            if currentcharread != "_"
                status = false
                break
            end
        end
    end
      
    return status
end

def addpoints(currentplayer, points)
  
    #print " points to be added " << points.to_s
  
    #print " current player current points before adding is" << currentplayer.points.to_s
  
    currentplayer.points = currentplayer.points.to_i + points.to_i
  
    #print " " << currentplayer.playername << " total points now after adding is " << currentplayer.points.to_s

    return currentplayer
end

def checkCharExists(gametext, searchchar)
    found = false
  
    #puts gametext.length
    # Iterate over the strings with "each."
  
    arrtextcount = 0
    arrcharcount = 0

    gametext.each do |st|
        arrtextcount = arrtextcount+ 1
        arrcharcount = 0
        st.each_char do |currentcharread|
            arrcharcount = arrcharcount+ 1
          
            if currentcharread == searchchar
                found = true
              
                gametext[arrtextcount-1][arrcharcount-1] = "_"
              
                return found
            end
      
        end
  
  
    end
  
  
    return found
end

#MAIN PROGRAM STARTS HERE
STDOUT.sync = true
gametext = [

"Cat in The Hat",
"Grand Rapids Fall Festival",
"Introduction to Programming",
"Chocolate Chip Cookies",
"Fancy 2018 Cars"

    ]

# Iterate over the strings with "each."

gettextcount gametext

player1 = Player. new("player1", 0)
player2 = Player. new("player2", 0)


currentplayer = player1
gameisover = false
allcharactersread = false
totalloops =0

while gameisover == false do

   gametext.each do |st|
      
        st.each_char do |currentcharread|
          
            totalloops = totalloops + 1
          
            guessedletter = ""
            print " CURRENT PLAYER=" << currentplayer.playername <<
            " GUESS A LETTER ="
          
            guessedstring = $stdin.gets.to_s
            guessedletter=""
  
            if guessedstring.length > 0
                print " LETTER GUESSED = " << guessedletter = guessedstring[0]
            else
                print " LETTER WAS NOT GUESSED"
                gameisover = true
                break
            end
  
  
            pointstobeadded = 1
            #search for the letter
           if(guessedletter != "" && (checkCharExists gametext , guessedletter) == true)
                print " LETTER FOUND "
              
                #add points to current player
                currentplayer = addpoints currentplayer,pointstobeadded
              
            else
                print " LETTER NOT FOUND "
              
                #add points to other player
                if (currentplayer.playername.eql?"player1") then
              
                #    print "current points is" << player2.points.to_s
                  
                    player2 = addpoints player2,pointstobeadded
                else
                    #print "current points is" << player1.points.to_s
                    player1 = addpoints player1,1
                end
              
                #change player in case he replies false
                if (currentplayer.playername == "player1") then
                    currentplayer = player2
                else
                    currentplayer = player1
                end
            end
          
            display_text gametext
            display_points(player1, player2)
          
        end  
      
        if gameisover == true
            break
        end
    end
  
    if (gameisover == false)
        #set flag if all characters are read
        gameisover = isgameover gametext
    end
  
  
end

if gameisover == true
        print " GAME over"
      
        print "Total turns played were " << totalloops.to_s
      
        display_points(player1, player2)
        #print " " << player1.playername << "=" << player1.points.to_s
        #print " " << player2.playername << "=" << player2.points.to_s
else
        print " PROGRAM over before GAME was ended"
end

#PROGRAM ENDS HERE

INPUT VALUES

W
h
a
t
a
p
r
o
g
r
a
m


C
a
t

i
n

T
h
e

H
a
t

I
s
'
n
t

t
h
a
t

g
r
e
a
t
G
r
a
n
d

R
a
p
i
d
s

F
a
l
l

F
e
s
t
i
v
a
l

I
n
t
r
o
d
u
c
t
i
o
n

t
o

P
r
o
g
r
a
m
m
i
n
g

C
h
o
c
o
l
a
t
e

C
h
i
p

C
o
o
k
i
e
s

F
a
n
c
y

2
0
1
8

C
a
r
s


OUTPUT

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote