Created
January 19, 2015 02:39
-
-
Save troyleach/ceaf5f332872df8bd38a to your computer and use it in GitHub Desktop.
Connect Four
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This is the basic game. I feel as if this is a total hack job at the moment. BUT it all works | |
| # I will spend free time to refactor and make it better piece of code for sure. | |
| # for this to run there are two other files that are needed these can be found on my git hub. | |
| # https://github.com/troyleach/Connect-Four | |
| require_relative 'player' | |
| @board = Array.new(6, Array.new(7, "-")) | |
| header = Array("1".."7") | |
| @board.unshift(header) | |
| @flipped = @board.transpose | |
| wait | |
| reset_screen! | |
| center_it | |
| puts "Your starting board".center(center_it) | |
| @boards = @flipped.transpose | |
| @board.each do |column| | |
| puts column.map { |row| row }.join(" ") | |
| end | |
| puts | |
| def place_game_piece(player) | |
| puts "#{player.name} your turn" | |
| while true | |
| print "Enter a column number or type 'q': " | |
| input = gets.chomp.downcase | |
| if input == "1" || input == "2" || input == "3" || input == "4" || input == "5" || input == "6" || input == "7" | |
| if input == "1" | |
| num = @flipped.length - 1 | |
| while num >= 0 | |
| if @flipped[0][num] == '-' | |
| @flipped[0][num] = player.piece | |
| break | |
| end | |
| num -= 1 | |
| end | |
| reset_screen! | |
| @boards = @flipped.transpose | |
| puts "Your game board".center(center_it) | |
| @boards.each do |column| | |
| puts column.map { |row| row }.join(" ") | |
| end | |
| break | |
| end | |
| if input == "2" | |
| num = @flipped.length - 1 | |
| while num >= 0 | |
| if @flipped[1][num] == '-' | |
| @flipped[1][num] = player.piece | |
| break | |
| end | |
| num -= 1 | |
| end | |
| reset_screen! | |
| @boards = @flipped.transpose | |
| puts "Your game board".center(center_it) | |
| @boards.each do |column| | |
| puts column.map { |row| row }.join(" ") | |
| end | |
| break | |
| end | |
| if input == "3" | |
| num = @flipped.length - 1 | |
| while num >= 0 | |
| if @flipped[2][num] == '-' | |
| @flipped[2][num] = player.piece | |
| break | |
| end | |
| num -= 1 | |
| end | |
| reset_screen! | |
| @boards = @flipped.transpose | |
| puts "Your game board".center(center_it) | |
| @boards.each do |column| | |
| puts column.map { |row| row }.join(" ") | |
| end | |
| break | |
| end | |
| if input == "4" | |
| num = @flipped.length - 1 | |
| while num >= 0 | |
| if @flipped[3][num] == '-' | |
| @flipped[3][num] = player.piece | |
| break | |
| end | |
| num -= 1 | |
| end | |
| reset_screen! | |
| @boards = @flipped.transpose | |
| puts "Your game board".center(center_it) | |
| @boards.each do |column| | |
| puts column.map { |row| row }.join(" ") | |
| end | |
| break | |
| end | |
| if input == "5" | |
| num = @flipped.length - 1 | |
| while num >= 0 | |
| if @flipped[4][num] == '-' | |
| @flipped[4][num] = player.piece | |
| break | |
| end | |
| num -= 1 | |
| end | |
| reset_screen! | |
| @boards = @flipped.transpose | |
| puts "Your game board".center(center_it) | |
| @boards.each do |column| | |
| puts column.map { |row| row }.join(" ") | |
| end | |
| break | |
| end | |
| if input == "6" | |
| num = @flipped.length - 1 | |
| while num >= 0 | |
| if @flipped[5][num] == '-' | |
| @flipped[5][num] = player.piece | |
| break | |
| end | |
| num -= 1 | |
| end | |
| reset_screen! | |
| @boards = @flipped.transpose | |
| puts "Your game board".center(center_it) | |
| @boards.each do |column| | |
| puts column.map { |row| row }.join(" ") | |
| end | |
| break | |
| end | |
| if input == "7" | |
| num = @flipped.length - 1 | |
| while num >= 0 | |
| if @flipped[6][num] == '-' | |
| @flipped[6][num] = player.piece | |
| break | |
| end | |
| num -= 1 | |
| end | |
| reset_screen! | |
| @boards = @flipped.transpose | |
| puts "Your game board".center(center_it) | |
| @boards.each do |column| | |
| puts column.map { |row| row }.join(" ") | |
| end | |
| break | |
| end | |
| elsif input == 'q' | |
| reset_screen! | |
| puts "I'm so sorry you decided to go, 'GOOD-BYE!" | |
| puts | |
| exit | |
| else | |
| puts "\nI don't understand that request!" | |
| end #ends the first if statement | |
| end #this ends the until loop | |
| end #this ends the method | |
| #- - - - - - - - - - - - - - - - - - - - - - | |
| # CHECKING FOR WINNER | |
| #- - - - - - - - - - - - - - - - - - - - - - | |
| def check_for_game_over(boards) | |
| #CHECKING FOR A TIE | |
| temp_array = [] | |
| if boards.flatten.select { |cell| temp_array << cell if cell == "-" } | |
| if temp_array.length == 0 | |
| puts "This game is a tie" | |
| puts "You both suck at this game!!!" | |
| puts "GOOD-BYE!" | |
| exit | |
| end | |
| end | |
| # CHECKING COLUMNS FOR WINNER | |
| if boards.transpose.map do |col| | |
| return true if col.join().match(/X{4}|O{4}/) | |
| end | |
| end | |
| # CHECKING ROWS FOR WINNER | |
| if boards.map do |row| | |
| return true if row.join().match(/X{4}|O{4}/) | |
| end | |
| end | |
| # CHECKING DIAGONAL UPPER LEFT TO BOTTOM RIGHT - - - - - - - - - - - - | |
| if temp_array = (3..6).collect { |i| boards[i][-3+i] } | |
| return true if temp_array.join().match(/X{4}|O{4}/) | |
| end | |
| if temp_array = (2..6).collect { |i| boards[i][-2+i] } | |
| return true if temp_array.join().match(/X{4}|O{4}/) | |
| end | |
| #return true if (1..6).collect { |i| boards[i][i-1] }.join().match(/X{4}|O{4}/) | |
| if temp_array = (1..6).collect { |i| boards[i][i-1] } | |
| return true if temp_array.join().match(/X{4}|O{4}/) | |
| end | |
| if temp_array = (1..6).collect { |i| boards[i][0+i] } | |
| return true if temp_array.join().match(/X{4}|O{4}/) | |
| end | |
| if temp_array = (1..5).collect { |i| boards[i][1+i] } | |
| return true if temp_array.join().match(/X{4}|O{4}/) | |
| end | |
| if temp_array = (1..4).collect { |i| boards[i][2+i] } | |
| return true if temp_array.join().match(/X{4}|O{4}/) | |
| end | |
| # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
| # CHECKING DIAGONAL UPPER RIGHT TO BOTTOM LEFT | |
| if temp_array = (1..6).collect { |i| boards[i][0-i] } | |
| return true if temp_array.join().match(/X{4}|O{4}/) | |
| end | |
| if temp_array = (2..6).collect { |i| boards[i][1-i] } | |
| return true if temp_array.join().match(/X{4}|O{4}/) | |
| end | |
| #return true if (3..6).collect { |i| boards[i][2-i] }.join().match(/X{4}|O{4}/) | |
| if temp_array = (3..6).collect { |i| boards[i][2-i] } | |
| return true if temp_array.join().match(/X{4}|O{4}/) | |
| end | |
| if temp_array = (1..6).collect { |i| boards[i][-1-i] } | |
| return true if temp_array.join().match(/X{4}|O{4}/) | |
| end | |
| if temp_array = (1..5).collect { |i| boards[i][-2-i] } | |
| return true if temp_array.join().match(/X{4}|O{4}/) | |
| end | |
| if temp_array = (1..4).collect { |i| boards[i][3-i] } | |
| return true if temp_array.join().match(/X{4}|O{4}/) | |
| end | |
| end | |
| #- - - - - - - - - - - - - - - - - - - - - - | |
| begin | |
| #print "hit return to continue or 'q' to quit: " | |
| #quit = gets.chomp.downcase | |
| #if quit == 'q' | |
| # reset_screen! | |
| # puts "Oh no, you don't want to play Connect Four? GOOD BYE!" | |
| # break | |
| #end | |
| @players.cycle do |player| | |
| place_game_piece(player) | |
| #check_for_game_over(@boards) | |
| if check_for_game_over(@boards) == true | |
| puts | |
| puts "WINNER WINNER CHICKEN DINNER" | |
| sleep(0.9) | |
| puts | |
| puts "Nice job #{player.name}, YOU ARE THE WINNER!!!!" | |
| break | |
| end | |
| end | |
| end while check_for_game_over(@boards) == nil | |
| puts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Once again, you went above and beyond which is AMAZING. Keep up the good work, and refactor away!