Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 8.52 KB | None | 0 0
  1. # https://www.primarysite-kidszone.co.uk/kidszone/resources/connect4.htm
  2. # Ruby code file - All your code should be located between the comments provide
  3. require 'sinatra'
  4. # Add any additional gems and global variables here
  5.         # remove '#' character to run sinatra wen server
  6.  
  7. # Main class module
  8. module MS_Game
  9.     # Input and output constants processed by subprocesses. MUST NOT change.
  10.     TOKEN1 = "O" # mine found
  11.     TOKEN2 = "X" # mine not found
  12.    
  13.     class Game
  14.  
  15.         attr_accessor :matrix, :player1, :player2, :hotspots, :input, :output, :turn, :turnsleft, :winner, :played, :score, :resulta, :resultb, :guess
  16.  
  17.         def initialize(input, output)
  18.             @input = input
  19.             @output = output
  20.         end
  21.        
  22.         def getinput
  23.             txt = @input.gets.chomp
  24.             return txt
  25.         end
  26.        
  27.         # Any code/methods aimed at passing the RSpect tests should be added below.
  28.         def created_by
  29.             return "Dimitris,Kleon"
  30.         end
  31.        
  32.         def student_id
  33.             return "51876736,51876391"
  34.         end
  35.  
  36.         def start()
  37.             @output.puts "Welcome to Minesweeper!"
  38.             @output.puts "Created by:#{created_by}"
  39.             @output.puts 'Game started.'
  40.             @output.puts "Enter a coordinate to uncover whether mine found."
  41.         end
  42.         def displaybegingame
  43.             @output.puts 'Begin game.'
  44.        
  45.         end
  46.         def displaynewgamecreated
  47.             @output.puts 'New game created.'
  48.         end
  49.  
  50.         def finish
  51.             @output.puts 'Game finished.'
  52.         end
  53.  
  54.         def displaymenu
  55.             @output.puts "Menu: (1)Start | (2)New | (9)Exit\n"
  56.         end
  57.         #instance variables set to 0
  58.         def clearscores
  59.             @resulta = 0
  60.             @resultb = 0
  61.         end
  62.         def displayplayerscores
  63.             @output.puts "Player 1: #{@resulta} and Player 2: #{@resultb}"
  64.         end
  65.         #ask player 1 for input
  66.         def displayplayer1prompt
  67.             @output.puts "Player 1 enter coordinate (0 returns to menu)."
  68.             p1_prompt = getinput
  69.             return p1_prompt
  70.         end
  71.         #ask player 2 for input
  72.         def displayplayer2prompt
  73.             @output.puts "Player 2 enter coordinate (0 returns to menu)."
  74.             p2_prompt = getinput
  75.             return p2_prompt
  76.         end
  77.         def displayinvalidinputerror
  78.             @output.puts 'Invalid input.'
  79.         end
  80.         def displaynomoreroomerror
  81.             @output.puts 'No more room.'
  82.         end
  83.         def displaywinner(p)
  84.             @output.puts "Player #{p} wins."
  85.         end
  86.         #set token of player 1 to X
  87.         def setplayer1
  88.             @player1 = "X"
  89.             return @player1
  90.         end
  91.         #set token of player 2 to x
  92.         def setplayer2
  93.             @player2 = "X"
  94.             return @player2
  95.         end
  96.         #gets the token of player 1
  97.         def getplayer1
  98.             location = setplayer1
  99.             return location
  100.         end
  101.         #gets the token of player 2
  102.         def getplayer2
  103.             location = setplayer2
  104.             return location
  105.         end
  106.         #initializes matrix
  107.         def clearcolumns_t
  108.             matrix = []
  109.             for i in 0..6
  110.                 matrix[i] = ["_", "_", "_", "_", "_", "_","_"]
  111.             end
  112.             return matrix
  113.         end
  114.         def clearcolumns
  115.             matrix = []
  116.             for i in 0..6
  117.                 matrix[i] = ["_", "_", "_", "_", "_", "_","_"]
  118.             end
  119.             @matrix = matrix
  120.             return @matrix
  121.         end
  122.         #gets a column value at a given position
  123.         def getcolumnvalue(x,y)
  124.             value = @matrix[x][y]
  125.             return value
  126.         end
  127.         #set a matrix position to v
  128.         def setmatrixcolumnvalue(c,  i, v)
  129.             if @matrix[c][i] == "_"
  130.                 @matrix[c][i] = v
  131.             end
  132.         end
  133.         def initialise_prompt
  134.             @output.puts "Game initialising..."
  135.             @output.puts "Created by #{created_by}"
  136.             @output.puts "Student ID: #{student_id}"
  137.             @output.puts "Game initialised."
  138.             @output.puts "Enter coordinate to uncover whether mine found."
  139.         end
  140.         #displays the matrix in a fancy way
  141.         def displayemptyframe
  142.             matrix = clearcolumns
  143.             bar = "|"
  144.             title = " 1 2 3 4 5 6 7 "
  145.             rowdivider = " +-+-+-+-+-+-+-+"
  146.             rowFempty = "1" + bar + matrix[1].join(bar) + bar
  147.             rowEempty = "2" + bar + matrix[2].join(bar) + bar
  148.             rowDempty = "3" + bar + matrix[3].join(bar) + bar
  149.             rowCempty = "4" + bar + matrix[4].join(bar) + bar
  150.             rowBempty = "5" + bar + matrix[5].join(bar) + bar
  151.             rowAempty = "6" + bar + matrix[6].join(bar) + bar
  152.             @output.puts " " + "#{title}"
  153.             @output.puts "#{rowdivider}"
  154.             @output.puts "#{rowAempty}"
  155.             @output.puts "#{rowdivider}"
  156.             @output.puts "#{rowBempty}"
  157.             @output.puts "#{rowdivider}"
  158.             @output.puts "#{rowCempty}"
  159.             @output.puts "#{rowdivider}"
  160.             @output.puts "#{rowDempty}"
  161.             @output.puts "#{rowdivider}"
  162.             @output.puts "#{rowEempty}"
  163.             @output.puts "#{rowdivider}"
  164.             @output.puts "#{rowFempty}"
  165.             @output.puts "#{rowdivider}"
  166.         end
  167.         def initialised_matrix(matrix)
  168.             count = 0
  169.             x = 1
  170.             y = 0
  171.             index = 1
  172.             while x != 7
  173.                 matrix[x][y] = index.to_s
  174.                 index += 1
  175.                 y += 1
  176.                 if y == 7
  177.                     y = 0
  178.                     x += 1
  179.                 end
  180.             end
  181.             return matrix
  182.        
  183.    
  184.              
  185.         end
  186.         def save_game(env)
  187.             save_file = File.open("save_file.txt","w")
  188.             save_file.puts env
  189.             save_file.close
  190.             return save_file
  191.         end
  192.         def save_scores
  193.             save_scores  = File.open("save_scores.txt","w")
  194.             save_scores.puts @resulta
  195.             save_scores.puts @resultb
  196.             save_scores.close
  197.             return save_scores
  198.         end
  199.         def resume_game(old_matrix)
  200.             x = 0
  201.             y = 0
  202.             arr = clearcolumns_t
  203.             save_file = File.open("save_file.txt","r")
  204.             save_file.each do |el|
  205.                 old_matrix[x][y] = el.chomp
  206.                 y += 1
  207.                 if y == 7
  208.                     y = 0
  209.                     x += 1
  210.                 end
  211.             end
  212.             save_file.close()
  213.             return old_matrix
  214.         end
  215.         def resume_scores
  216.             arr_score = []
  217.             scores = File.open("save_scores.txt","r")
  218.             scores.each do |line|
  219.                 arr_score.push(line)
  220.             end
  221.             scores.close()
  222.             @resulta = arr_score[0]
  223.             @resultb = arr_score[1]
  224.             return @resulta,@resultb
  225.         end
  226.  
  227.            
  228.         def displaymatrix(matrix)
  229.             bar = "|"
  230.             title = " 1 2 3 4 5 6 7 "
  231.             puts "  " + "#{title}"
  232.             puts "1" + " " + bar + matrix[1].join(bar) + bar
  233.             puts "2" + " " + bar + matrix[2].join(bar) + bar
  234.             puts "3" + " " + bar + matrix[3].join(bar) + bar
  235.             puts "4" + " " + bar + matrix[4].join(bar) + bar
  236.             puts "5" + " " + bar + matrix[5].join(bar) + bar
  237.             puts "6" + " " + bar + matrix[6].join(bar) + bar
  238.         end
  239.         #generate mines on random positions within the matrix
  240.         def generatemines
  241.             pos = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42]
  242.             hotspots = []
  243.             for i in 0..41 do
  244.                 j = pos.size-1
  245.                 r = rand(0..j)
  246.                 hotspots.push(pos[r])
  247.                 pos.delete_at(r)
  248.                 j = j - 1
  249.             end
  250.             @hotspots = hotspots
  251.             return @hotspots
  252.         end
  253.  
  254.         def playgame(environment,mines)
  255.             player1_input = displayplayer1prompt.to_i
  256.             player2_input = displayplayer2prompt.to_i
  257.             match_p1 = nil
  258.             match_p2 = nil
  259.             if mines.include?(player1_input)
  260.                 @resulta += 1
  261.                 match_p1 = true
  262.                 mines.delete(player1_input)
  263.             end
  264.             if mines.include?(player2_input)
  265.                 @resultb += 1
  266.                 match_p2 = true
  267.                 mines.delete(player2_input)
  268.             end
  269.             environment.each do |rows|
  270.                 rows.each_with_index do |number,index|
  271.                     rows[index] = "O" if number == player1_input.to_s && match_p1 == true
  272.                     rows[index] = "X" if number == player1_input.to_s && match_p1 == nil
  273.                     rows[index] = "O" if number == player2_input.to_s && match_p2 == true
  274.                     rows[index] = "X" if number == player2_input.to_s && match_p2 == nil
  275.        
  276.                 end
  277.             end
  278.         end
  279.         def enter_difficulty
  280.             puts "Enter game difficulty (easy | medium | hard)"
  281.             difficulty = gets.chomp
  282.             if difficulty == "easy"
  283.                 mines = generatemines[1..15]
  284.             elsif difficulty == "medium"
  285.                 mines = generatemines[1..8]
  286.             elsif difficulty == "hard"
  287.                 mines = generatemines[1..5]
  288.             else
  289.                 displayinvalidinputerror
  290.                 puts "Game has been set by default to easy mode."
  291.                 mines = generatemines[1..15]
  292.             return mines
  293.             end
  294.         end
  295.  
  296.        
  297.  
  298.  
  299.         #checks the winner
  300.         def check_winner
  301.             @winner = nil
  302.             end_game = nil
  303.             if @resulta == 3 && @resulta > @resultb
  304.                 @winner = 1
  305.                 @output.puts "Player #{@winner} wins!."
  306.                 @output.puts "Here are the final results => Player1 #{@resulta} : Player2 #{@resultb}"
  307.                 end_game = true
  308.                 return end_game
  309.             elsif @resultb == 3 && @resultb >@resulta
  310.                 @winner = 2
  311.                 @output.puts "Player #{@winner} wins!."
  312.                 @output.puts "Here are the final results => Player1 #{@resulta} : Player2 #{@resultb}"
  313.                 end_game = true
  314.                 return end_game
  315.             elsif @resulta == 3 && @resultb == 3 && @resulta == @resultb
  316.                 @winner = nil
  317.                 @output.puts "It's a draw!"
  318.                 @output.puts "Here are the final results => Player1 #{@resulta} : Player2 #{@resultb}"
  319.                 end_game = true
  320.  
  321.             end
  322.  
  323.         end
  324.         def checkwinner
  325.             if @resulta >= 3
  326.                 @winner = 1
  327.             elsif @resultb>= 3
  328.                 @winner = 2
  329.             else
  330.                 @winner = nil
  331.             end
  332.             return @winner
  333.         end
  334.        
  335.     end
  336. end
  337.  
  338. # Any code/methods aimed at passing the RSpect tests should be added above.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement