generated from Code-Institute-Org/python-essentials-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstacks.py
38 lines (31 loc) · 1008 Bytes
/
stacks.py
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
"""
Oscar Nieves, in his article 'Programming a Connect-4 game on Python'
suggests using Stacks as a data structure for the 7 columns in
Connect4. His code for defining a Stack class is copied here.
https://oscarnieves100.medium.com/programming-a-connect-4-game-on-python-f0e787a3a0cf
A game of connect-4 Versus the computer
Author: Oscar A. Nieves
Updated: August 9, 2021
"""
class Stack:
""" Class stack for each column """
def __init__(self):
self._list = []
def __len__(self):
return len(self._list)
def push(self, element):
""" Add element to top of stack if there is room """
if len(self._list) <= 6:
self._list.append(element)
else:
return
def peek(self):
""" Get last element of stack """
return self._list[-1]
# Code by MR
def build_empty_cols(columns):
""" Initialises empty column Stacks """
for col in range(7):
col = Stack()
columns.append(col)
return columns