Added ship placement logic without checks for existing ships. Need to add existing ship check.

This commit is contained in:
Todd Kaiser
2021-03-02 19:32:02 -07:00
parent cad8dd6f66
commit 0a4e66a6e7

View File

@@ -15,12 +15,8 @@
"orig_nbformat": 2,
"kernelspec": {
"name": "python3",
"display_name": "Python 3.7.3 64-bit ('python': venv)",
"metadata": {
"interpreter": {
"hash": "d99f099520c2505e9f745916a84fcd3ec6a6f02f993bb8e12b586a85fced6daa"
}
}
"display_name": "Python 3",
"language": "python"
}
},
"nbformat": 4,
@@ -28,7 +24,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 225,
"metadata": {},
"outputs": [],
"source": [
@@ -37,7 +33,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 226,
"metadata": {},
"outputs": [],
"source": [
@@ -54,14 +50,14 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 227,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"5 2\n"
"7 2\n"
]
}
],
@@ -72,25 +68,25 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 228,
"metadata": {},
"outputs": [],
"source": [
"def random_direction(valid_directions):\n",
" idx = random.ran range(len(valid_directions))\n",
" idx = random.randrange(len(valid_directions))\n",
" return valid_directions[idx]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 229,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1\n"
"0\n"
]
}
],
@@ -100,7 +96,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 230,
"metadata": {},
"outputs": [],
"source": [
@@ -134,7 +130,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 231,
"metadata": {},
"outputs": [
{
@@ -156,7 +152,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 232,
"metadata": {},
"outputs": [],
"source": [
@@ -187,14 +183,17 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 233,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"('BATTLESHIP', 5) 5 7 6 [(5, 7), (5, 6), (5, 5), (5, 4), (5, 3)]\n('CRUISER', 3) 1 3 3 [(1, 3), (2, 4), (3, 5)]\n('DESTROYER<A>', 2) 5 9 6 [(5, 9), (5, 8)]\n('DESTROYER<B>', 2) 3 4 3 [(3, 4), (4, 5)]\n"
"('BATTLESHIP', 5) 2 2 4 [(2, 2), (3, 2), (4, 2), (5, 2), (6, 2)]\n",
"('CRUISER', 3) 6 7 3 [(6, 7), (7, 8), (8, 9)]\n",
"('DESTROYER<A>', 2) 1 1 3 [(1, 1), (2, 2)]\n",
"('DESTROYER<B>', 2) 8 3 6 [(8, 3), (8, 2)]\n"
]
}
],
@@ -208,6 +207,110 @@
" print(SHIPS[ship],x,y,direction,coords)"
]
},
{
"cell_type": "code",
"execution_count": 234,
"metadata": {},
"outputs": [],
"source": [
"computer_board = [ [ -1 for y in range(BOARD_WIDTH)] \n",
" for x in range(BOARD_HEIGHT)]\n",
"player_board = [ [ -1 for y in range(BOARD_WIDTH)] \n",
" for x in range(BOARD_HEIGHT)]"
]
},
{
"cell_type": "code",
"execution_count": 235,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
" 1 2 3 4 5 6 7 8 9 10\n",
" 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n",
" 2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n",
" 3 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n",
" 4 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n",
" 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n",
" 6 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n",
" 7 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n",
" 8 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n",
" 9 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n",
"10 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n"
]
}
],
"source": [
"def print_board(board):\n",
"\n",
" print(' ',end='')\n",
" for z in range(BOARD_WIDTH):\n",
" print(f'{z+1:3}',end='')\n",
" print('')\n",
"\n",
" for x in range(len(board)):\n",
" print(f'{x+1:2}',end='')\n",
" for y in range(len(board[x])):\n",
" print(f\"{board[x][y]:3}\",end='')\n",
" print('')\n",
"\n",
"print_board(computer_board)"
]
},
{
"cell_type": "code",
"execution_count": 236,
"metadata": {},
"outputs": [],
"source": [
"def place_ship(board,coords,ship):\n",
" for coord in coords:\n",
" print(\"--->\",coord)\n",
" board[coord[0]-1][coord[1]-1] = 0"
]
},
{
"cell_type": "code",
"execution_count": 237,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[(7, 5), (6, 4), (5, 3), (4, 2), (3, 1)]\n",
"---> (7, 5)\n",
"---> (6, 4)\n",
"---> (5, 3)\n",
"---> (4, 2)\n",
"---> (3, 1)\n",
" 1 2 3 4 5 6 7 8 9 10\n",
" 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n",
" 2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n",
" 3 0 -1 -1 -1 -1 -1 -1 -1 -1 -1\n",
" 4 -1 0 -1 -1 -1 -1 -1 -1 -1 -1\n",
" 5 -1 -1 0 -1 -1 -1 -1 -1 -1 -1\n",
" 6 -1 -1 -1 0 -1 -1 -1 -1 -1 -1\n",
" 7 -1 -1 -1 -1 0 -1 -1 -1 -1 -1\n",
" 8 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n",
" 9 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n",
"10 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n"
]
}
],
"source": [
"x,y = random_x_y()\n",
"ship = 0\n",
"directions = get_possible_directions(x,y,ship)\n",
"direction = random_direction(directions)\n",
"coords = generate_ship_coordinates(x,y,direction,ship)\n",
"print(coords)\n",
"place_ship(computer_board,coords,0)\n",
"print_board(computer_board)"
]
},
{
"cell_type": "code",
"execution_count": null,