Skip to content

First-Kira/Connect-Four

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Sure, let's break down this Connect Four game step by step:

  1. Initialization (initialize method):

    • When a new instance of the ConnectFour class is created, the initialize method sets up the game.
    • It creates a 6x7 grid representing the game board using a 2D array (@board). Each cell is initially filled with a space character.
    • It sets the current player (@current_player) to 'X'.
  2. Printing the Board (print_board method):

    • The print_board method displays the current state of the game board on the console.
    • It prints the column numbers at the top and draws horizontal lines to separate the rows.
    • It iterates through each row of the board, printing the contents of each cell with vertical separators.
  3. Dropping a Piece (drop_piece method):

    • The drop_piece method takes a column number as input.
    • It checks if the column is valid (between 1 and 7) and if there is space in the column to drop a piece.
    • If the column is valid and there is space, it drops the current player's piece into the lowest available position in that column.
  4. Switching Players (switch_player method):

    • The switch_player method switches the current player between 'X' and 'O' after each turn.
  5. Checking for a Win (check_win method):

    • The check_win method examines the current state of the board to see if there is a winning condition.
    • It checks for four consecutive pieces horizontally, vertically, and diagonally (both up-right and down-right).
  6. Playing the Game (play method):

    • The play method orchestrates the gameplay loop.
    • It repeatedly prints the board, prompts the current player to make a move, and processes the move.
    • If a player wins, it announces the winner and ends the game.
    • If the board is full without a winner, it declares a draw and ends the game.
    • Otherwise, it switches the player and continues the loop.
  7. Instantiating and Starting the Game:

    • Finally, the script creates an instance of the ConnectFour class (game) and calls the play method to start the game.

Overall, this code implements the logic and mechanics of a basic Connect Four game in Ruby.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages