My first small game with Python
Published: 2022-04-22 14:12:48

As a part of my computer science study at Codecademy, we have to build a terminal game.

The choice was not easy at all, but I decided to take one of the suggestions they gave us in the project description which was the (connect four game ^_^).

The game looks easy to build if I used Javascript which I am more familiar with, but using python was a really challenge for me.

## Game description:

- The game is called Connect Four:
  
![the game](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wtggckr4f4cf1d0r6ad8.gif)



- The game is played on a grid that has six rows and seven columns.
- It has two players, X and Y, who alternate turns.
- Each player has a different color, Red and Green.
- When a player drops a piece, it lands in the lowest available space in the column.
- The next player continues until either four of the same pieces are connected vertically, horizontally, or diagonally.


- The game ends when either player has four in a row.
- The game also ends when the board is full and neither player has four in a row.
- When the game ends, the game can be restarted.

---
- Win vertical:




![Win vertical](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fgw8c7waegyrtq1h40bn.gif)



---
- Win diagonal:
  


![Win diagonal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5elnpjok2f0zlb2u0df.gif)


---
- The game has also validations:



![validations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oxk9j14qioxhuzac0ohd.gif)
  

---

The most tricky part was checking for diagonally win, but at the end was fixed using the following function:

```python
def check_circles_in_row_angle(player):
    letters_list = ["A", "B", "C", "D", "E", "F", "G"]
    numbers_list = [1, 2, 3, 4, 5, 6]
    global is_won
    is_won = False
    circles = player.circles
    circles.sort(key=lambda circle: circle.letter)  # sort circles by letter
    string_circles = [f"{circle.letter}{circle.number}" for circle in circles]
    if len(circles) >= 4:
        for circle in circles:
            letter = circle.letter
            number = circle.number
            global compare_one
            compare_one = True
            list_to_compare_1 = []
            list_to_compare_2 = []
            global letters_list_to_compare
            global numbers_list_to_compare
            if number < 4:
                numbers_list_to_compare = numbers_list[numbers_list.index(number) :]
            elif number >= 4:
                numbers_list_to_compare = numbers_list[::-1]
                numbers_list_to_compare = numbers_list_to_compare[
                    numbers_list_to_compare.index(number) :
                ]

            # === letter < D ======"
            if letter < "D":
                letters_list_to_compare = letters_list[letters_list.index(letter) :]

                i = 0
                while i < 4:
                    list_to_compare_1.append(
                        f"{letters_list_to_compare[i]}{numbers_list_to_compare[i]}"
                    )
                    i += 1
            # === letter > D ======
            if letter > "D":
                letters_list_to_compare = letters_list[::-1]
                letters_list_to_compare = letters_list_to_compare[
                    letters_list_to_compare.index(letter) :
                ]

                i = 0
                while i < 4:
                    list_to_compare_1.append(
                        f"{letters_list_to_compare[i]}{numbers_list_to_compare[i]}"
                    )
                    i += 1

            # === letter == D ======
            if letter == "D":

                letters_list_to_compare = letters_list[letters_list.index(letter) :]

                i = 0
                while i < 4:
                    list_to_compare_1.append(
                        f"{letters_list_to_compare[i]}{numbers_list_to_compare[i]}"
                    )
                    i += 1

                letters_list_to_compare = letters_list[::-1]
                letters_list_to_compare = letters_list_to_compare[
                    letters_list_to_compare.index(letter) :
                ]

                i = 0
                while i < 4:
                    list_to_compare_2.append(
                        f"{letters_list_to_compare[i]}{numbers_list_to_compare[i]}"
                    )
                    i += 1

            x = 0
            global all_in
            all_in = True
            while x < len(list_to_compare_1):
                if not list_to_compare_1[x] in string_circles:
                    all_in = False
                    break

                x += 1

            if all_in:
                is_won = True
                return is_won

            if len(list_to_compare_2) > 0:
                y = 0
                while y < len(list_to_compare_2):
                    if not list_to_compare_2[y] in string_circles:
                        all_in = False
                        break

                    y += 1

            if all_in:
                is_won = True
                return is_won
    return is_won
```

## Conclusion:

Building this small game was really helpful for me to understand a lot of concepts in programming in addition to learning a lot of python.


Source code:
[GitHub](https://github.com/mohammadalnajar/connect_four_game_py)