Advertisement
Guest User

Untitled

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