Python beginner here--working on a Battleship project for a class. My program will loop through the first player to place their ships. The grid does successfully print the visual ships on the board, but once i get to player two, the ships are overlapping with player one. (also note there is some validation work that still needs to be done).
I think the issue is that maybe I'm storing both coordinates for each player in the same list. So my question is how do I store the values for each player so that I can get the board to print ships only applicable to each player?
Here are my classes:
BOARD_SIZE = 10
class Ship:
def __init__(self, ship_name, size, coords, player, direction):
self.ship_name = ship_name
self.size = size
self.player = player
self.coords = coords
self.direction = direction
class Board:
def __init__(self):
self.board = []
self.guesses = []
board = [['O']*BOARD_SIZE for _ in range(BOARD_SIZE)]
def add_ship(self, name, size, player, coords, direction):
for coord in coords:
# convert string like "a1" to x,y coordinates
y = ord(coord[0])-ord('a')
x = int(coord[1:])-1
# update the board at this position
self.board = board[x][y]
print(" " + " ".join([chr(c) for c in range(ord('A'), ord('A') + BOARD_SIZE)]))
row_num = 1
for row in board:
print(str(row_num).rjust(2) + " " + (" ".join(row)))
row_num += 1
self.board.append(Ship(coords,player,name,size,direction))
def print_board_heading(self):
print(" " + " ".join([chr(c) for c in range(ord('A'), ord('A') + BOARD_SIZE)]))
def print_board(self):
board = [['O']*BOARD_SIZE for _ in range(BOARD_SIZE)]
print_board_heading()
row_num = 1
for row in board:
print(str(row_num).rjust(2) + " " + (" ".join(row)))
row_num += 1
def print_updated_board(coords, direction, board, player):
for coord in coords:
# convert string like "a1" to x,y coordinates
y = ord(coord[0])-ord('a')
x = int(coord[1:])-1
# update the board at this position
board[x][y] = '|' if direction == 'v' else '-'
print(" " + " ".join([chr(c) for c in range(ord('A'), ord('A') + BOARD_SIZE)]))
row_num = 1
for row in board:
print(str(row_num).rjust(2) + " " + (" ".join(row)))
row_num += 1
class Player():
def __init__(self,name):
self.name = name
self.board = Board()
self.ships = []
self.guesses = []
And the battleship game file:
from ship import Ship, Player, Board
SHIP_INFO = [
("Aircraft Carrier", 5),
("Battleship", 4),
("Submarine", 3),
("Cruiser", 3),
("Patrol Boat", 2)
]
BOARD_SIZE = 10
VERTICAL_SHIP = '|'
HORIZONTAL_SHIP = '-'
EMPTY = 'O'
MISS = '.'
HIT = '*'
SUNK = '#'
board = [['O']*BOARD_SIZE for _ in range(BOARD_SIZE)]
def print_board_heading():
print(" " + " ".join([chr(c) for c in range(ord('A'), ord('A') + BOARD_SIZE)]))
def print_board():
board = [['O']*BOARD_SIZE for _ in range(BOARD_SIZE)]
print_board_heading()
row_num = 1
for row in board:
print(str(row_num).rjust(2) + " " + (" ".join(row)))
row_num += 1
def print_updated_board(coords, direction,board):
# create an empty board
# board = [['O']*BOARD_SIZE for _ in range(BOARD_SIZE)]
# at each coordinate, draw a ship
for coord in coords:
# convert string like "a1" to x,y coordinates
y = ord(coord[0])-ord('a')
x = int(coord[1:])-1
# update the board at this position
board[x][y] = '|' if direction == 'v' else '-'
print_board_heading()
row_num = 1
for row in board:
print(str(row_num).rjust(2) + " " + (" ".join(row)))
row_num += 1
def clear_screen():
print("\033c", end="")
def get_coordinates(ship):
while True:
print("\n")
coordinate = input("Where do you want the " + ship + "(example: A1)?: ")
coords_strip = coordinate.strip()
coords_lower = coords_strip.lower()
x = coords_lower[0]
y = coords_lower[1:]
if (len(x)+len(y)) in range(2,4):
if x not in 'abcdefghij' or y not in '1,2,3,4,5,6,7,8,9,10':
print("Oops! That was not a valid entry. Try again...")
continue
else:
return x, y
else:
if len(coords_lower) < 2 or len(coords_lower) > 3:
print("Oops! That's too not the right amount of characters. Please try again...")
continue
def get_direction():
while True:
dir = input("[H]orizontal or [V]ertical?: ")
dir_strip = dir.strip()
direction = dir_strip.lower()
if direction not in 'hv':
print("Oops! That was not a valid entry. Try again...")
continue
else:
return direction
def create_ship_coordinates(x, y, size, direction):
ship_col = ord(x)
ship_row = int(y)
if direction == 'v':
# ship runs vertically DOWN from coordinate
coords = [chr(ship_col) + str(r) for r in range(ship_row, ship_row + size)]
return coords
else:
# ship runs horizontally RIGHT from coordinate
coords = [chr(col) + str(ship_row) for col in range(ship_col, ship_col + size)]
return coords
def place_user_ships(player):
ships = []
print_board()
print("\n")
print("Let's go " + player + " !")
for ship, size in SHIP_INFO:
while True:
# ask for starting coordinate
x, y = get_coordinates(ship)
# ask for vertical or horizontal direction
direction = get_direction()
# create the coordinates for the ship placement
coords = create_ship_coordinates(x, y, size, direction)
# validate the
# new_ship = Board().add_ship(ship, size, coords, direction, player)
# update = Board.print_updated_board(coords,direction,board,player)
break
# create ship from data
# add the ship from above to a player list
# player = Player(board)
# place the ship on the game board
# print out the board to reflect the shp placement
clear_screen()
print("\n")
input("All ships placed for {}. Please hit ENTER to continue....".format(player))
player1 = input("What's Player 1's Name? ")
player2 = input("What's Player 2's Name? ")
# define player one's fleet
place_user_ships(player1)
place_user_ships(player2)
IIRC, in battleship there are really four boards. One is managed by each player with their shots at the enemy, and the other is the player's own ships and incoming shots from the enemy.
The "shot" process is one of notifying the enemy where the shot was taken, the enemy responding with "hit" or "miss", and recording the result on the local player's outgoing-shots board.
The "notification" process is one of receiving a location where an enemy shot was made, looking up the result on the local-ships board, returning "hit" or "miss", and updating the local ships board to indicate the enemy shot.
So you have a Player, with a pair of Boards. You might also have a Game class to marry the two players.
Inputting the ships is going to depend a lot on your actual user interface. Are you doing graphics with a mouse? Are you doing text with a mouse? Text with arrow keys via curses or some such? Simple coordinate inputs?
If you're doing coordinates, you probably want something simple, like x,y,{up,down,left,right} to eliminate having to position every block of the ship.
Again, there is a Board method here - placing a ship. The board can enforce whatever rules you have about placement (ex: can two ships be directly adjacent? Or must there be a one-space gap between?) and reject the inappropriate attempts.
If you put all the wisdom in your Board class, then the Player classes can be simple, with just links to the boards. And the game can manage the players in turn:
def play(self):
p1 = self.player[0]
p2 = self.player[1]
try:
while True:
self.attack(p1, p2)
self.attack(p2, p1)
except Victory v:
print("The winner is", v.player.name)
I notice you doing things like player1 = input("What is player 1's name?"). This is something that should be pushed into the player classes.
Try to design from the top down: game, player, board. And try following the rule, "Tell, don't ask." That is, if something needs to be done, you tell a class to do it, instead of asking the class for data and doing it yourself.
Instead of this:
move = player1.get_move()
if valid_move(move):
...
Do this:
player1.make_move()
And push the logic downwards. Eventually, you get to the place where the knowledge "should be". The player "should" make moves. The board "should" know what is a valid move or not. And every method "should" be given enough data to do its job. ;-)
Related
I'm new to python and writing my first project. I'm trying to implement a check that if a space is already occupied, not to move there. I can't seem to figure out why my move_player method overwrites the index value of the board even though I am explicitly checking it (If it is O's turn and X has already been placed in the index O is trying to move to, it just overwrites it). I have tried hard coding the check for 'X' and 'O' instead of player.player as well but can't seem to figure it out. Does it have to do something with how Python works or am I implementing it wrong?
class Player:
def __init__(self, player):
self.player = player
class Board:
def __init__(self):
self.board = [[' ' for i in range(3)] for j in range(3)]
def display_board(self):
print('---------')
for row in self.board:
print('| ', end='')
for col in row:
print(f'{col} ', end='')
print('|')
print('---------')
def move_player(self, player):
try:
p1 = Player('X')
p2 = Player('O')
coordinates = [int(i) for i in input("Enter coordinates for move: ").split()]
xCoordinate = coordinates[0]
yCoordinate = coordinates[1]
if ((self.board[xCoordinate][yCoordinate] == p1.player) or
(self.board[xCoordinate][yCoordinate] == p2.player)):
print("That space is occupied, please choose another one.")
self.move_player(player)
else:
self.board[xCoordinate - 1][yCoordinate - 1] = player.player
except (ValueError, IndexError):
print("Please only enter numbers between 1 and 3.")
self.move_player(player)
def has_won(self, player):
if self.check_diagonal(player):
return True
elif self.check_across(player):
return True
elif self.check_down(player):
return True
return False
if __name__ == '__main__':
board = Board()
player1 = Player('X')
player2 = Player('O')
player = player1
while True:
board.display_board()
board.move_player(player)
if board.has_won(player):
board.display_board()
print(f'{player.player} wins!!!')
break
if player == player1:
player = player2
else:
player = player1
The code is very convoluted but from what I can see:
if ((self.board[xCoordinate][yCoordinate] == p1.player) or
(self.board[xCoordinate][yCoordinate] == p2.player)):
...
self.board[xCoordinate - 1][yCoordinate - 1] = player.player
You are checking [x,y] but assigning to [x-1,y-1].
Im currently trying to add a player to a board in python to be able to command the player but I cant seem to figure it out.
def GenPlayer(level):
player = {}
player['name'] = GenName()
player['history'] = GenHistory() #history
player['attack'] = DieRoller(3,6) + DieRoller(level,4)
player['defense'] = DieRoller(3,6) + DieRoller(level,4)
player['health'] = DieRoller(5,6) + DieRoller(level,4)
player['row'] = 0
player['col'] = 0
character =(player['name']) + ("Health: "+str(player['health'])) + (" Defense: "+str(player['defense'])) + (" Attack: "+str(player['attack']))
return character
def CreateBoard():
board = []
while True:
x = int(input("Number of Rows: "))
y = x
break
for row in range(0,x):
board.append(["()"]*y)
return board
def ShowBoard(board):
for row in board:
print(" ".join(row))
def PlacePlayer(board,player):
row = DieRoller(1,6)
col = DieRoller(1,6)
board[row][col] = 'player'
return board,player
print(PlacePlayer(ShowBoard(CreateBoard()),GenPlayer(1)))
I keep getting this error when trying this code:
Exception has occurred: TypeError
'NoneType' object is not subscriptable
If you want to place the player object at the selected position, remove the quote marks:
board[row][col] = player
Update:
A player object is a dictionary, so you need to place the player['name'] on the board to be able to print it in the ShowBoard() function you have, as the function uses .join() , which is a string method. I made a slight change to PlacePlayer(), and simplified the code to make it a bit easier to understand what's happening:
def PlacePlayer(board,player):
row = 5
col = 4
board[row][col] = player['name']
return board
newboard= CreateBoard()
characterDescripiton , playerobject = GenPlayer(1)
boardafterPlacePlayer = PlacePlayer(newboard,playerobject)
ShowBoard(boardafterPlacePlayer)
Good Day. I'm a beginner in coding, and basically I made a simple maze game using python.
it's 3x3 so I made 9 dictionary variables that detailed the possible movement for each tile (North, south, east, west.)
my code consists of nested if-elif as well as a main while loop.
if level == 1: #level of difficulty.
i = 0
maplist1 = {'directions1':'south'}
maplist2 = {'directions1':'south','directions2':'east'}
maplist3 = {'directions1':'west'}
maplist4 = {'directions1':'north','directions2':'east','directions3':'south'}
....
....
current_position = 1 #local variable within the first if statement
Here's a snippet of the code block that would be repeated, with little to no variations (literally repeating) to it.
while i == 0: #to continuously loop the program
if current_position == 1:
directions = maplist1['directions1']
direction = input(f"What direction do you want to go: {directions} ").title() #choose the possible directions to go to.
if direction == "S" or direction == "South":
current_position = 4
print(f"You are in cell {current_position}.")
else:
print("Cannot go in that direction.")
if current_position == 2:
directions = maplist2['directions1']
directions2 = maplist2['directions2']
direction = input(f"What direction do you want to go: {directions} {directions2} ").title()
if direction == "S" or direction == "South":
current_position = 5
print(f"You are in cell {current_position}.")
elif direction == "E" or direction == "East":
current_position = 3
print(f"You are in cell {current_position}.")
else:
print("Cannot go in that direction.")
if current_position == 3:
directions = maplist3['directions1']
direction = input(f"What direction do you want to go: {directions} ").title()
if direction == "W" or direction == "West":
current_position = 2
print(f"You are in cell {current_position}.")
else:
print("Cannot go in that direction.")
if current_position == 4:
directions = maplist4['directions1']
directions2 = maplist4['directions2']
directions3 = maplist4['directions3']
direction = input(f"What direction do you want to go: {directions} {directions2} {directions3} ").title()
if direction == "N" or direction == "North":
current_position = 1
print(f"You are in cell {current_position}.")
elif direction == "S" or direction == "South":
current_position = 7
print(f"You are in cell {current_position}.")
elif direction == "E" or direction == "East":
current_position = 5
print(f"You are in cell {current_position}.")
else:
print("Cannot go in that direction.")
.......
......
.......
if current_position == 9:
print("Congratulations, you finished the game.")
i += 1 #to stop the loop
my question is how to make this simpler, and more compact?
Current location must be remembered every movement
I know how to use def but quite unsure on how to implement it in my maze game.
There are 3 levels to this game, basically 3x3, 4x4, and 5x5. that's the reason why I am asking for any ideas on how to shorten/compact the code.
I don't need you to give me your code, but some guidance would be great on how to proceed because i am feeling lost right now.
Try creating a maze data structure. One typical idea is to create a 2D array of cells, each with a north/west/south/east wall.
Let's create a class, just for better understanding. I want you to ignore everything except the can_move() method.
class Cell:
def __init__(self, walls):
self.walls = walls
def __str__(self):
"""Allows us to call print() on a Cell!"""
return '\n'.join(self.draw())
def _draw_wall(self, wall, s):
return s if wall in self.walls else ' ' * len(s)
def draw(self, inner=' '):
"""Draw top, mid, and bottom parts of the cell."""
n = self._draw_wall('N', '___')
w = self._draw_wall('W', '|')
e = self._draw_wall('E', '|')
s = self._draw_wall('S', '___')
top = ' ' + n + ' '
mid = w + inner + e
bot = w + s + e
return top, mid, bot
def can_move(self, direction):
return direction not in self.walls
Let's make some cells and see what they look like:
>>> Cell('NWSE')
___
| |
|___|
>>> Cell('NWS')
___
|
|___
Now, a maze is made out of a 2D list of cells. Here's a small maze:
maze = Maze(width=3, height=3, rows=[
[Cell('NWE'), Cell('NW'), Cell('NE')],
[Cell('WE'), Cell('WE'), Cell('WE')],
[Cell('WS'), Cell('SE'), Cell('WSE')]
])
Which looks like this:
>>> maze
___ ___ ___
| x || |
| || |
| || || |
| || || |
| || |
|___ ___||___|
But wait! We haven't defined what Maze is. Once again, ignore all the drawing related stuff and focus on move_direction().
class Maze:
def __init__(self, width, height, rows):
self.width = width
self.height = height
self.rows = rows
self.position = (0, 0)
def __str__(self):
return '\n'.join(self.draw_row(i) for i, _ in enumerate(self.rows))
def _inner(self, i, j):
return ' x ' if (i, j) == self.position else ' '
def draw_row(self, i):
triples = [
cell.draw(self._inner(i, j)) for j, cell in enumerate(self.rows[i])
]
return '\n'.join([
''.join(t for t, _, _ in triples),
''.join(m for _, m, _ in triples),
''.join(b for _, _, b in triples)])
def cell_at_position(self, i, j):
return self.rows[i][j]
def move_direction(self, direction):
curr_cell = self.cell_at_position(*self.position)
if not curr_cell.can_move(direction):
print("Can't go in that direction!")
return
deltas = {'N': (-1, 0), 'W': (0, -1), 'S': (1, 0), 'E': (0, 1)}
y, x = self.position
dy, dx = deltas[direction]
self.position = y + dy, x + dx
To use this, just create a simple small loop:
while True:
print(maze)
direction = input('What direction? ')
maze.move_direction(direction)
And watch the magic happen!
You can try it here.
Instead of thinking the maze in 1D i.e the current_position(1,2,3..,9), you should think and code it like a 2D matrix space((0,0),(0,1)).
And Instead of hard coding the base maze to 3, we should be thinking in terms of n x n maze, as now that you have implemented the logic for a 3 x 3, now you should be able to expand it to n x n.
And you handled the possible directions using your maps instead, we should write the logic for boundary conditions, like a method to check the directions possible on each step,
def possible_path(current_x, current_y, matrix_size=3):
# this returns like directions possible like current_x + 1 < matrix_size
# then the east direction is possible.
pass
Direction handling should be handled somewhat like this,
def position_handler(row, col, direction):
# can write the logic for mapping the direction to the movement in X and Y,
# if direction == 'NORTH'
# or can get the config from a map like,
# move = movement.get(direction)
# say NORTH config is =>movement= {'NORTH': {'x': 0, 'y':1}}
# and now move accordingly i.e
# row += move.get('x')
# col += move.get('y')
pass
Exploit rules instead of hardcoding anything.
If you have a 3x3 maze, your (x,y) indexes are somthing like
0,0 1,0 2,0
0,1 1,1 2,1
0,2 1,2 2,2
You can store this as list of lists:
field = [ ["one","two","three"],["four","five","six"],["seven","eight","nine"] ]
pos = (0,0)
p = field[pos[0]][pos[1]] # would be "one"
# rules for movement based on actual position:
move_right = pos[0] < (3-1)
move_left = pos[0] > 0
move_up = pos[1] > 0
move_down = pos[1] < (3-1)
You can use functions to code this:
# p is a coordinate (0,0) etc tuple of x and y
# as functions
def can_move_right(p, dim=3):
return p[0] < dim-1
def can_move_left(p,dim=3):
return p[0] > 0
def can_move_up(p, dim=3):
return p[1] > 0
def can_move_down(p,dim=3):
return p[1] < dim-1
def move_up(p):
if can_move_up(p):
return (p[0],p[1]-1)
# implicitly returns None if not possible
def move_down(p):
if can_move_down(p):
return (p[0],p[1]+1)
# etc
new_pos = move_up( (1,1) ) # => (1,0)
new_pos = move_down( (1,1) ) # => (1,2)
new_pos = move_up( (0,0) ) # None - need to handle it
if not new_pos:
print("Wrong move")
To adjust for n x n you just need to give other dim - the rules are the same.
I have an excercise to do and I'm stuck. It's the board game Alak, not much known, that I have to code in python. I can link the execrcise with the rules so you can help me better. The code has the main part and the library with all the procedures and function.
from Library_alak import *
n = 0
while n < 1:
n = int(input('Saisir nombre de case strictement positif : '))
loop = True
player = 1
player2 = 2
removed = [-1]
board = newboard(n)
display(board, n)
while loop:
i = select(board, n, player, removed)
print(i)
board = put(board, player, i)
display(board, n)
capture(board, n, player, player2)
loop = True if again(board, n, player, removed) is True else False
if player == 1 and loop:
player, player2 = 2, 1
elif player == 2 and loop:
player, player2 = 1, 2
win(board, n)
print(win(board, n))
And here is the library:
def newboard(n):
board = ([0] * n)
return board
def display(board, n):
for i in range(n):
if board[i] == 1:
print('X', end=' ')
elif board[i] == 2:
print('O', end=' ')
else:
print(' . ', end=' ')
def capture(board, n, player, player2):
for place in range(n):
if place == player:
place_beginning = place
while board[place] != player:
place_end = place
if board[place + x] == player:
return board
else:
return board
def again(board, n, player, removed):
for p in board(0):
if p == 0:
if p not in removed:
return True
else:
return False
def possible(n, removed, player, i, board):
for p in range(n + 1):
if p == 1:
if board[p-1] == 0:
if p not in removed:
return True
else:
return False
def win(board, n):
piecesp1 = 0
piecesp2 = 0
for i in board(0):
if i == 1:
piecesp1 += 1
else:
piecesp2 += 1
if piecesp1 > piecesp2:
print('Victory : Player 1')
elif piecesp2 > piecesp1:
print('Victory : Player 2')
else:
return 'Equality'
def select(board, n, player, removed):
loop = True
while loop:
print('player', player)
i = int(input('Enter number of boxes : '))
loop = False if possible(n, removed, player, i, board)is True else True
return i
def put(board, player, i):
i -= 1
if board[i] == 0:
if player == 1:
board[i] = 1
return board
else:
board[i] = 2
return board
else:
put(board, player, i)
So my problems here are that I have few errors, the first one is that when I enter the number '1' when asked to enter a number of boxes ( which is the place to play on ) nothing happens. Then when entering any other number, either the error is : if board[place + x] == player:
NameError: name 'x' is not defined
or there seems to be a problem with the : if board[place + x] == player:
NameError: name 'x' is not defined
I would appreciate a lot if anyone could help me. I'm conscious that it might not be as detailed as it should be and that you maybe don't get it all but you can contact me for more.
Rules of the Alak game:
Black and white take turns placing stones on the line. Unlike Go, this placement is compulsory if a move is available; if no move is possible, the game is over.
No stone may be placed in a location occupied by another stone, or in a location where a stone of your own colour has just been removed. The latter condition keeps the game from entering a neverending loop of stone placement and capture, known in Go as ko.
If placing a stone causes one or two groups of enemy stones to no longer have any adjacent empty spaces--liberties, as in Go--then those stones are removed. As the above rule states, the opponent may not play in those locations on their following turn.
If placing a stone causes one or two groups of your own colour to no longer have any liberties, the stones are not suicided, but instead are safe and not removed from play.
You shouldn't use "player2" as a variable, there's an easier way, just use "player" which take the value 1 or 2 according to the player. You know, something like that : player = 1 if x%2==0 else 2
and x is just a increasing int from 0 until the end of the game.
I am trying to write a program for connect 4 but am having a lot of trouble getting past the directions. Everything under the comment, "#everything works up to here" works but then it all explodes and I have no idea even where to start to fix it.
#connect 4
import random
#define global variables
X = "X"
O = "O"
EMPTY = "_"
TIE = "TIE"
NUM_ROWS = 6
NUM_COLS = 8
def display_instruct():
"""Display game instructions."""
print(
"""
Welcome to the second greatest intellectual challenge of all time: Connect4.
This will be a showdown between your human brain and my silicon processor.
You will make your move known by entering a column number, 1 - 7. Your move
(if that column isn't already filled) will move to the lowest available position.
Prepare yourself, human. May the Schwartz be with you! \n
"""
)
def ask_yes_no(question):
"""Ask a yes or no question."""
response = None
while response not in ("y", "n"):
response = input(question).lower()
return response
def ask_number(question,low,high):
"""Ask for a number within range."""
#using range in Python sense-i.e., to ask for
#a number between 1 and 7, call ask_number with low=1, high=8
low=1
high=NUM_COLS
response = None
while response not in range (low,high):
response=int(input(question))
return response
def pieces():
"""Determine if player or computer goes first."""
go_first = ask_yes_no("Do you require the first move? (y/n): ")
if go_first == "y":
print("\nThen take the first move. You will need it.")
human = X
computer = O
else:
print("\nYour bravery will be your undoing... I will go first.")
computer = X
human = O
return computer, human
def new_board():
board = []
for x in range (NUM_COLS):
board.append([" "]*NUM_ROWS)
return board
def display_board(board):
"""Display game board on screen."""
for r in range(NUM_ROWS):
print_row(board,r)
print("\n")
def print_row(board, num):
"""Print specified row from current board"""
this_row = board[num]
print("\n\t| ", this_row[num], "|", this_row[num], "|", this_row[num], "|", this_row[num], "|", this_row[num], "|", this_row[num], "|", this_row[num],"|")
print("\t", "|---|---|---|---|---|---|---|")
# everything works up to here!
def legal_moves(board):
"""Create list of column numbers where a player can drop piece"""
legal=True
while not legal:
col = input("What column would you like to move into (1-7)?")
for row in range (6,0,1):
if (1 <= row <= 6) and (1 <= col <= 7) and (board[row][col]==" "):
board[row][col] = turn
legal = True
else:
print("Sorry, that is not a legal move.")
def human_move(board,human):
"""Get human move"""
try:
legals = legal_moves(board)
move = None
while move not in legals:
move = ask_number("Which column will you move to? (1-7):", 1, NUM_COLS)
if move not in legals:
print("\nThat column is already full, nerdling. Choose another.\n")
print("Human moving to column", move)
return move #return the column number chosen by user
except NameError:
print ("Only numbers are allowed.")
except IndexError:
print ("You can only select colums from 1-7.")
def get_move_row(turn,move):
for m in (NUM_COLS):
place_piece(turn,move)
display_board()
def computer_move ():
move= random.choice(legal)
return move
def place_piece(turn,move):
if this_row[m[move]]==" ":
this_row.append[m[move]]=turn
def winner(board):
# Check rows for winner
for row in range(6):
for col in range(3):
if (board[row][col] == board[row][col + 1] == board[row][col + 2] == board[row][col + 3]) and (board[row][col] != " "):
return [row][col]
# Check columns for winner
for col in range(6):
for row in range(3):
if (board[row][col] == board[row + 1][col] == board[row + 2][col] ==board[row + 3][col]) and (board[row][col] != " "):
return [row][col]
# Check diagonal (top-left to bottom-right) for winner
for row in range(3):
for col in range (4):
if (board[row][col] == board[row + 1][col + 1] == board[row + 2][col + 2] == board[row + 3][col + 3]) and (board[row][col] != " "):
return true
# Check diagonal (bottom-left to top-right) for winner
for row in range (5,2,-1):
for col in range (3):
if (board[row][col] == board[row - 1][col + 1] == board[row - 2][col + 2] == board[row - 3][col + 3]) and (board[row][col] != " "):
return [row][col]
# No winner
return False
def main():
display_instruct()
computer,human = pieces()
turn = X
board = new_board()
while not winner(board) and (" " not in board):
display_board(board)
if turn == human:
human_move(board,human)
get_move_row()
place_piece()
else:
computer_move(board,computer)
place_piece()
display_board(board)
turn = next_turn()
the_winner = winner(board)
congrat_winner(the_winner, computer, human)
#start the program
main ()
input ("\nPress the enter key to quit.")
For fun, here's an object-oriented refactorization. It's a bit long, but well documented and should be easy to understand.
I started with your code and split it into Board, Player, and Game classes, then derived Computer and Human classes from Player.
Board knows the shape and size of the rack, what moves are legal, and recognizes when wins and ties occur
Player has a name and knows how to choose (or prompt for) a legal move
Game has a Board and two Players and controls turn-taking and output
I'm not 100% happy with it - Board has a .board that is a list of list of string, but Game has a .board that is a Board; a bit of judicious renaming would be a good idea - but for an hour's work it's pretty solid.
Hope you find this educational:
# Connect-4
from itertools import cycle, groupby
from random import choice
from textwrap import dedent
import sys
# version compatibility shims
if sys.hexversion < 0x3000000:
# Python 2.x
inp = raw_input
rng = xrange
else:
# Python 3.x
inp = input
rng = range
def get_yn(prompt, default=None, truthy={"y", "yes"}, falsy={"n", "no"}):
"""
Prompt for yes-or-no input
Return default if answer is blank and default is set
Return True if answer is in truthy
Return False if answer is in falsy
"""
while True:
yn = inp(prompt).strip().lower()
if not yn and default is not None:
return default
elif yn in truthy:
return True
elif yn in falsy:
return False
def get_int(prompt, lo=None, hi=None):
"""
Prompt for integer input
If lo is set, result must be >= lo
If hi is set, result must be <= hi
"""
while True:
try:
value = int(inp(prompt))
if (lo is None or lo <= value) and (hi is None or value <= hi):
return value
except ValueError:
pass
def four_in_a_row(tokens):
"""
If there are four identical tokens in a row, return True
"""
for val,iterable in groupby(tokens):
if sum(1 for i in iterable) >= 4:
return True
return False
class Board:
class BoardWon (BaseException): pass
class BoardTied(BaseException): pass
EMPTY = " . "
HOR = "---"
P1 = " X "
P2 = " O "
VER = "|"
def __init__(self, width=8, height=6):
self.width = width
self.height = height
self.board = [[Board.EMPTY] * width for h in rng(height)]
self.tokens = cycle([Board.P1, Board.P2])
self.rowfmt = Board.VER + Board.VER.join("{}" for col in rng(width)) + Board.VER
self.rule = Board.VER + Board.VER.join(Board.HOR for col in rng(width)) + Board.VER
def __str__(self):
lines = []
for row in self.board:
lines.append(self.rowfmt.format(*row))
lines.append(self.rule)
lines.append(self.rowfmt.format(*("{:^3d}".format(i) for i in rng(1, self.width+1))))
lines.append("")
return "\n".join(lines)
def is_board_full(self):
return not any(cell == Board.EMPTY for cell in self.board[0])
def is_win_through(self, row, col):
"""
Check for any winning sequences which pass through self.board[row][col]
(This is called every time a move is made;
thus any win must involve the last move,
and it is faster to check just a few cells
instead of the entire board each time)
"""
# check vertical
down = min(3, row)
up = min(3, self.height - row - 1)
tokens = [self.board[r][col] for r in rng(row - down, row + up + 1)]
if four_in_a_row(tokens):
return True
# check horizontal
left = min(3, col)
right = min(3, self.width - col - 1)
tokens = [self.board[row][c] for c in rng(col - left, col + right + 1)]
if four_in_a_row(tokens):
return True
# check upward diagonal
down = left = min(3, row, col)
up = right = min(3, self.height - row - 1, self.width - col - 1)
tokens = [self.board[r][c] for r,c in zip(rng(row - down, row + up + 1), rng(col - left, col + right + 1))]
if four_in_a_row(tokens):
return True
# check downward diagonal
down = right = min(3, row, self.width - col - 1)
up = left = min(3, self.height - row - 1, col)
tokens = [self.board[r][c] for r,c in zip(rng(row - down, row + up + 1), rng(col + right, col - left - 1, -1))]
if four_in_a_row(tokens):
return True
# none of the above
return False
def legal_moves(self):
"""
Return a list of columns which are not full
"""
return [col for col,val in enumerate(self.board[0], 1) if val == Board.EMPTY]
def do_move(self, column):
token = next(self.tokens)
col = column - 1
# column is full?
if self.board[0][col] != Board.EMPTY:
next(self.move) # reset player token
raise ValueError
# find lowest empty cell (guaranteed to find one)
for row in rng(self.height-1, -1, -1): # go from bottom to top
if self.board[row][col] == Board.EMPTY: # find first empty cell
# take cell
self.board[row][col] = token
# did that result in a win?
if self.is_win_through(row, col):
raise Board.BoardWon
# if not, did it result in a full board?
if self.is_board_full():
raise Board.BoardTied
# done
break
class Player:
def __init__(self, name):
self.name = name
def get_move(self, board):
"""
Given the current board state, return the row to which you want to add a token
"""
# you should derive from this class instead of using it directly
raise NotImplemented
class Computer(Player):
def get_move(self, board):
return choice(board.legal_moves())
class Human(Player):
def get_move(self, board):
legal_moves = board.legal_moves()
while True:
move = get_int("Which column? (1-{}) ".format(board.width), lo=1, hi=board.width)
if move in legal_moves:
return move
else:
print("Please pick a column that is not already full!")
class Game:
welcome = dedent("""
Welcome to the second greatest intellectual challenge of all time: Connect4.
This will be a showdown between your human brain and my silicon processor.
You will make your move known by entering a column number, 1 - 7. Your move
(if that column isn't already filled) will move to the lowest available position.
Prepare yourself, human. May the Schwartz be with you!
""")
def __init__(self):
print(Game.welcome)
# set up new board
self.board = Board()
# set up players
self.players = cycle([Human("Dave"), Computer("HAL")])
# who moves first?
if get_yn("Do you want the first move? (Y/n) ", True):
print("You will need it...\n")
# default order is correct
else:
print("Your rashness will be your downfall...\n")
next(self.players)
def play(self):
for player in self.players:
print(self.board)
while True:
col = player.get_move(self.board) # get desired column
try:
print("{} picked Column {}".format(player.name, col))
self.board.do_move(col) # make the move
break
except ValueError:
print("Bad column choice - you can't move there")
# try again
except Board.BoardWon:
print("{} won the game!".format(player.name))
return
except Board.BoardTied:
print("The game ended in a stalemate")
return
def main():
while True:
Game().play()
if not get_yn("Do you want to play again? (Y/n) ", True):
break
if __name__=="__main__":
main()