10000 Feature/integrate by KelsonDenton · Pull Request #13 · NM-TAFE/battleships · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Feature/integrate #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions clients/KelsonD/terminal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@




class GameBoard:
def __init__(self, moves_dictionary):
for num in range(1,11):
moves_dictionary = dict(x="A", y=num, status="clear")
for num in range(1,11):
moves_dictionary = dict(x="B", y=num, status="clear")
for num in range(1,11):
moves_dictionary = dict(x="C", y=num, status="clear")


if __name__=='__main__':
for num in range(1, 11):
moves_dictionary = dict(x="A", y=num, status="clear")
for num in range(1, 11):
moves_dictionary = dict(x="B", y=num, status="clear")
for num in range(1, 11):
moves_dictionary = dict(x="C", y=num, status="clear")
for item,y in moves_dictionary:
print(item)
2 changes: 1 addition & 1 deletion clients/reference/app/battleships_pb2_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""Client and server classes corresponding to protobuf-defined services."""
import grpc

import battleships_pb2 as battleships__pb2
import clients.reference.app.battleships_pb2 as battleships__pb2


class BattleshipsStub(object):
Expand Down
245 changes: 245 additions & 0 deletions clients/reference/app/board.py

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions clients/reference/app/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import queue
import threading
import uuid
from battleships_pb2 import Attack, Request, Response, Status
from battleships_pb2_grpc import BattleshipsStub
from clients.reference.app.battleships_pb2 import Attack, Request, Response, Status
from clients.reference.app.battleships_pb2_grpc import BattleshipsStub

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
Expand Down Expand Up @@ -128,8 +128,8 @@ def attack(self, vector):
:param vector: Vector to send to game server, e.g., "G4"
:raise ValueError: if vector is None or not a string
"""
if vector is None or type(vector) is not str:
raise ValueError('Parameter vector must be a string!')
if vector is None or type(vector) is not int:
raise ValueError('Parameter vector must be a integer!')

self.__send(Request(move=Attack(vector=vector)))

Expand Down
8000
37 changes: 29 additions & 8 deletions clients/reference/app/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import threading
import time
from client import Battleship
from clients.reference.client.py import Battleship
from clients.reference.app.board import Board

grpc_host = os.getenv('GRPC_HOST', 'localhost')
grpc_port = os.getenv('GRPC_PORT', '50051')
Expand All @@ -11,15 +12,21 @@

battleship = Battleship(grpc_host=grpc_host, grpc_port=grpc_port)

game = Board()


@battleship.on()
def begin():
"""This callback is called when the game server indicates that a game
starts. This happens when two players are available to play. If you are
the first to register, you may have to wait for someone else to connect.
"""
print('Game started!')


@battleship.on()
def start_turn():
s = input('Your move> ')
s = game.my_turn()
battleship.attack(s)


Expand All @@ -35,38 +42,52 @@ def miss():

@battleship.on()
def win():
"""This callback indicates the other player was defeated.
"""
print('Yay! You won!')
playing.clear()


@battleship.on()
def lose():
"""Callback indicates you are defeated. This callback is called when you
call :meth: 'defeat' method on the Battleship client, so this will never
come as a surprise.
"""
print('Aww... You lost...')
playing.clear()


@battleship.on()
def attack(vector):
"""Callback indicates an attack by another player. It takes a single
argument which is the square that is attacked. Please note game server
does not validate the vector, so it is up to the clients to decide on a
certain format.
"""
vector = vector[0]
print(f'Shot received at {vector}')
while True:
print("""H)it, m)iss, or d)efeat?""")
s = input('Enter status> ')
_s = s[0].upper()
if _s == 'H':
# print("""H)it, m)iss, or d)efeat?""")
s = game.check_strike(vector)
if s == 'hit':
battleship.hit()
game.update_hit_board(s, vector)
break
elif _s == 'M':
elif s == 'miss':
battleship.miss()
game.update_hit_board(s, vector)
break
elif _s == 'D':
elif game.check_defeat():
battleship.defeat()
break
else:
continue


game.place_ships()
print('Waiting for the game to start...')
battleship.join()
while playing.is_set():
# giving processor a break
time.sleep(1.0)
6 changes: 6 additions & 0 deletions clients/reference/app/test_board.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from clients.reference.app.board import Board

test_game = Board()

if __name__ == '__main__':
test_game.place_ships()
58 changes: 58 additions & 0 deletions clients/reference/app/validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class MoveValidator:

# class takes coordinates, orient, spaces, and board as constructors
def __init__(self, coord, orient, spaces, board):
self.coordinate = coord
self.orientation = orient
self.space = spaces
self.playerBoard = board

# method that checks the user enters a valid input for orientation
def check_coord(self):
while self.coordinate < 0 or self.coordinate > 99 or s F438 elf.playerBoard[self.coordinate] == "■":
self.coordinate = int(input("Please enter a coordinate that is in range: "))
self.coordinate = self.check_coord()
return self.coordinate
else:
return self.coordinate

# method that checks the user entered a valid character for orientation
def check_orient(self):
while self.orientation not in {'H', 'V'}:
self.orientation = str(input("Please enter an orientation that is H)orizontal or V)ertical: "))
self.orientation = self.check_orient()
return self.orientation
else:
return self.orientation

# blah blah blah
# method that checks ensuing places ship is placed are all free
def check_spaces(self):
try:
all_clear = True
if self.orientation == 'V':
for i in range(1, self.space):
if self.playerBoard[self.coordinate + 10 * i] != "0":
all_clear = False
elif self.orientation == 'H':
for i in range(1, self.space):
if self.playerBoard[self.coordinate + i] != "0":
all_clear = False
while not all_clear:
self.coordinate = int(input('That piece falls on top of another. Pick another coordinate: '))
all_clear = True
self.check_spaces()
except:
self.coordinate = int(input('The piece you entered fell off the edge. Pick another coordinate: '))
self.check()
return

# method to check if piece attempts to wrap around board
def wrap_around(self):
pass

# method that puts together all other methods
def check(self):
self.coordinate = self.check_coord()
self.orientation = self.check_orient()
self.check_spaces()
2 changes: 1 addition & 1 deletion server/app/game.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import log
from server.app import log
from threading import Lock

logger = log.get_logger(__name__)
Expand Down
6 changes: 3 additions & 3 deletions server/app/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import grpc
import logging
import os
from battleships_pb2_grpc import add_BattleshipsServicer_to_server
from server.app.battleships_pb2_grpc import add_BattleshipsServicer_to_server
from concurrent.futures import ThreadPoolExecutor
from server import Battleship
import log
from server.app.server import Battleship
from server.app import log

logger = log.get_logger(__name__)
logger.setLevel(logging.DEBUG)
Expand Down
10 changes: 5 additions & 5 deletions server/app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import threading
import time
import uuid
import log
from battleships_pb2 import Attack, Response, Status
from battleships_pb2_grpc import BattleshipsServicer
from game import Game
from message import Message
from server.app import log
from server.app.battleships_pb2 import Attack, Response, Status
from server.app.battleships_pb2_grpc import BattleshipsServicer
from server.app.game import Game
from server.app.message import Message

logger = log.get_logger(__name__)
logger.setLevel(logging.DEBUG)
Expand Down
0