Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # https://www.primarysite-kidszone.co.uk/kidszone/resources/connect4.htm
- # Ruby code file - All your code should be located between the comments provided.
- # Add any additional gems and global variables here
- #require 'sinatra' # remove '#' character to run sinatra wen server
- # Main class module
- module MS_Game
- # Input and output constants processed by subprocesses. MUST NOT change.
- TOKEN1 = "O" # mine found
- TOKEN2 = "X" # mine not found
- class Game
- attr_accessor :matrix, :player1, :player2, :hotspots, :input, :output, :turn, :turnsleft, :winner, :played, :score, :resulta, :resultb, :guess
- def initialize(input, output)
- @input = input
- @output = output
- end
- def getinput
- txt = @input.gets.chomp
- return txt
- end
- # Any code/methods aimed at passing the RSpect tests should be added below.
- def created_by
- return "Dimitris,Kleon"
- end
- def student_id
- return "51876736,51876391"
- end
- def start()
- @output.puts "Welcome to Minesweeper!"
- @output.puts "Created by:#{created_by}"
- @output.puts 'Game started.'
- @output.puts "Enter a coordinate to uncover whether mine found."
- end
- def displaybegingame
- @output.puts 'Begin game.'
- end
- def displaynewgamecreated
- @output.puts 'New game created.'
- end
- def finish
- @output.puts 'Game finished.'
- end
- def displaymenu
- @output.puts "Menu: (1)Start | (2)New | (9)Exit\n"
- end
- #instance variables set to 0
- def clearscores
- @resulta = 0
- @resultb = 0
- end
- def displayplayerscores
- @output.puts "Player 1: #{@resulta} and Player 2: #{@resultb}"
- end
- #ask player 1 for input
- def displayplayer1prompt
- @output.puts "Player 1 enter coordinate (0 returns to menu)."
- p1_prompt = getinput
- return p1_prompt
- end
- #ask player 2 for input
- def displayplayer2prompt
- @output.puts "Player 2 enter coordinate (0 returns to menu)."
- p2_prompt = getinput
- return p2_prompt
- end
- def displayinvalidinputerror
- @output.puts 'Invalid input.'
- end
- def displaynomoreroomerror
- @output.puts 'No more room.'
- end
- def displaywinner(p)
- @output.puts "Player #{p} wins."
- end
- #set token of player 1 to X
- def setplayer1
- @player1 = "X"
- return @player1
- end
- #set token of player 2 to x
- def setplayer2
- @player2 = "X"
- return @player2
- end
- #gets the token of player 1
- def getplayer1
- location = setplayer1
- return location
- end
- #gets the token of player 2
- def getplayer2
- location = setplayer2
- return location
- end
- #initializes matrix
- def clearcolumns_t
- matrix = []
- for i in 0..6
- matrix[i] = ["_", "_", "_", "_", "_", "_","_"]
- end
- return matrix
- end
- def clearcolumns
- matrix = []
- for i in 0..6
- matrix[i] = ["_", "_", "_", "_", "_", "_","_"]
- end
- @matrix = matrix
- return @matrix
- end
- #gets a column value at a given position
- def getcolumnvalue(x,y)
- value = @matrix[x][y]
- return value
- end
- #set a matrix position to v
- def setmatrixcolumnvalue(c, i, v)
- if @matrix[c][i] == "_"
- @matrix[c][i] = v
- end
- end
- def initialise_prompt
- @output.puts "Game initialising..."
- @output.puts "Created by #{created_by}"
- @output.puts "Student ID: #{student_id}"
- @output.puts "Game initialised."
- @output.puts "Enter coordinate to uncover whether mine found."
- end
- #displays the matrix in a fancy way
- def displayemptyframe
- matrix = clearcolumns
- bar = "|"
- title = " 1 2 3 4 5 6 7 "
- rowdivider = " +-+-+-+-+-+-+-+"
- rowFempty = "1" + bar + matrix[1].join(bar) + bar
- rowEempty = "2" + bar + matrix[2].join(bar) + bar
- rowDempty = "3" + bar + matrix[3].join(bar) + bar
- rowCempty = "4" + bar + matrix[4].join(bar) + bar
- rowBempty = "5" + bar + matrix[5].join(bar) + bar
- rowAempty = "6" + bar + matrix[6].join(bar) + bar
- @output.puts " " + "#{title}"
- @output.puts "#{rowdivider}"
- @output.puts "#{rowAempty}"
- @output.puts "#{rowdivider}"
- @output.puts "#{rowBempty}"
- @output.puts "#{rowdivider}"
- @output.puts "#{rowCempty}"
- @output.puts "#{rowdivider}"
- @output.puts "#{rowDempty}"
- @output.puts "#{rowdivider}"
- @output.puts "#{rowEempty}"
- @output.puts "#{rowdivider}"
- @output.puts "#{rowFempty}"
- @output.puts "#{rowdivider}"
- end
- def initialised_matrix(matrix)
- count = 0
- x = 1
- y = 0
- index = 1
- while x != 7
- matrix[x][y] = index.to_s
- index += 1
- y += 1
- if y == 7
- y = 0
- x += 1
- end
- end
- return matrix
- end
- def save_game(env)
- save_file = File.open("save_file.txt","w")
- save_file.puts env
- save_file.close
- return save_file
- end
- def save_scores
- save_scores = File.open("save_scores.txt","w")
- save_scores.puts @resulta
- save_scores.puts @resultb
- save_scores.close
- return save_scores
- end
- def resume_game(old_matrix)
- x = 0
- y = 0
- arr = clearcolumns_t
- save_file = File.open("save_file.txt","r")
- save_file.each do |el|
- old_matrix[x][y] = el.chomp
- y += 1
- if y == 7
- y = 0
- x += 1
- end
- end
- save_file.close()
- return old_matrix
- end
- def resume_scores
- arr_score = []
- scores = File.open("save_scores.txt","r")
- scores.each do |line|
- arr_score.push(line)
- end
- scores.close()
- @resulta = arr_score[0]
- @resultb = arr_score[1]
- return @resulta,@resultb
- end
- def displaymatrix(matrix)
- bar = "|"
- title = " 1 2 3 4 5 6 7 "
- puts " " + "#{title}"
- puts "1" + " " + bar + matrix[1].join(bar) + bar
- puts "2" + " " + bar + matrix[2].join(bar) + bar
- puts "3" + " " + bar + matrix[3].join(bar) + bar
- puts "4" + " " + bar + matrix[4].join(bar) + bar
- puts "5" + " " + bar + matrix[5].join(bar) + bar
- puts "6" + " " + bar + matrix[6].join(bar) + bar
- end
- #generate mines on random positions within the matrix
- def generatemines
- 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]
- hotspots = []
- for i in 0..41 do
- j = pos.size-1
- r = rand(0..j)
- hotspots.push(pos[r])
- pos.delete_at(r)
- j = j - 1
- end
- @hotspots = hotspots
- return @hotspots
- end
- def playgame(environment,mines)
- player1_input = displayplayer1prompt.to_i
- player2_input = displayplayer2prompt.to_i
- match_p1 = nil
- match_p2 = nil
- if mines.include?(player1_input)
- @resulta += 1
- match_p1 = true
- mines.delete(player1_input)
- end
- if mines.include?(player2_input)
- @resultb += 1
- match_p2 = true
- mines.delete(player2_input)
- end
- environment.each do |rows|
- rows.each_with_index do |number,index|
- rows[index] = "O" if number == player1_input.to_s && match_p1 == true
- rows[index] = "X" if number == player1_input.to_s && match_p1 == nil
- rows[index] = "O" if number == player2_input.to_s && match_p2 == true
- rows[index] = "X" if number == player2_input.to_s && match_p2 == nil
- end
- end
- end
- def enter_difficulty
- puts "Enter game difficulty (easy | medium | hard)"
- difficulty = gets.chomp
- if difficulty == "easy"
- mines = generatemines[1..15]
- elsif difficulty == "medium"
- mines = generatemines[1..8]
- elsif difficulty == "hard"
- mines = generatemines[1..5]
- else
- displayinvalidinputerror
- puts "Game has been set by default to easy mode."
- mines = generatemines[1..15]
- return mines
- end
- end
- #checks the winner
- def check_winner
- @winner = nil
- end_game = nil
- if @resulta == 3 && @resulta > @resultb
- @winner = 1
- @output.puts "Player #{@winner} wins!."
- @output.puts "Here are the final results => Player1 #{@resulta} : Player2 #{@resultb}"
- end_game = true
- return end_game
- elsif @resultb == 3 && @resultb >@resulta
- @winner = 2
- @output.puts "Player #{@winner} wins!."
- @output.puts "Here are the final results => Player1 #{@resulta} : Player2 #{@resultb}"
- end_game = true
- return end_game
- elsif @resulta == 3 && @resultb == 3 && @resulta == @resultb
- @winner = nil
- @output.puts "It's a draw!"
- @output.puts "Here are the final results => Player1 #{@resulta} : Player2 #{@resultb}"
- end_game = true
- end
- end
- def checkwinner
- if @resulta >= 3
- @winner = 1
- elsif @resultb>= 3
- @winner = 2
- else
- @winner = nil
- end
- return @winner
- end
- end
- end
- # Any code/methods aimed at passing the RSpect tests should be added above.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement