{ "metadata": { "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3-final" }, "orig_nbformat": 2, "kernelspec": { "name": "python3", "display_name": "Python 3", "language": "python" } }, "nbformat": 4, "nbformat_minor": 2, "cells": [ { "cell_type": "code", "execution_count": 225, "metadata": {}, "outputs": [], "source": [ "import random" ] }, { "cell_type": "code", "execution_count": 226, "metadata": {}, "outputs": [], "source": [ "random.seed()\n", "\n", "BOARD_WIDTH = 10\n", "BOARD_HEIGHT = 10\n", "\n", "def random_x_y():\n", " x = random.randrange(1,BOARD_WIDTH+1)\n", " y = random.randrange(1,BOARD_HEIGHT+1)\n", " return (x,y)" ] }, { "cell_type": "code", "execution_count": 227, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "7 2\n" ] } ], "source": [ "x,y = random_x_y()\n", "print (x,y)" ] }, { "cell_type": "code", "execution_count": 228, "metadata": {}, "outputs": [], "source": [ "def random_direction(valid_directions):\n", " idx = random.randrange(len(valid_directions))\n", " return valid_directions[idx]" ] }, { "cell_type": "code", "execution_count": 229, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "0\n" ] } ], "source": [ "print(random_direction([0,1,2]))" ] }, { "cell_type": "code", "execution_count": 230, "metadata": {}, "outputs": [], "source": [ "SHIPS = [ (\"BATTLESHIP\", 5),\n", " (\"CRUISER\", 3),\n", " (\"DESTROYER\", 2),\n", " (\"DESTROYER\", 2) ]\n", "\n", "# given a coordinate (x,y) and a ship type,\n", "# determine which directions from the coordinate \n", "# a ship could be placed. Directions are numbered\n", "# starting at 0 for up, 1 for up/right, 2 for right, \n", "# etc., clockwise from zero (12 o'clock position)\n", "# returns a vector of direction numbers where\n", "# a ship can be placed, starting at one end of the\n", "# ship to its length\n", "def get_possible_directions(x,y,ship):\n", " ship_len = SHIPS[ship][1] - 1\n", " dirs = [False for x in range(8)]\n", " dirs[0] = (x - ship_len) >=1\n", " dirs[2] = (y + ship_len) <= BOARD_WIDTH\n", " dirs[1] = dirs[0] and dirs[2]\n", " dirs[4] = (x + ship_len) <= BOARD_HEIGHT\n", " dirs[3] = dirs[2] and dirs[4]\n", " dirs[6] = (y - ship_len) >= 1\n", " dirs[5] = dirs[4] and dirs[6]\n", " dirs[7] = dirs[6] and dirs[0]\n", "\n", " return [x for x in range(len(dirs)) if dirs[x]]" ] }, { "cell_type": "code", "execution_count": 231, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Boundary Conditions\n( 5, 5): [0, 1, 2, 3, 4, 5, 6, 7]\n( 1, 1): [2, 3, 4]\n( 1,10): [4, 5, 6]\n(10,10): [0, 6, 7]\n(10, 1): [0, 1, 2]\n" ] } ], "source": [ "print(\"Boundary Conditions\")\n", "print(\"( 5, 5):\",get_possible_directions(5,5,0))\n", "print(\"( 1, 1):\",get_possible_directions(1,1,0))\n", "print(\"( 1,10):\",get_possible_directions(1,10,0))\n", "print(\"(10,10):\",get_possible_directions(10,10,0))\n", "print(\"(10, 1):\",get_possible_directions(10,1,0))" ] }, { "cell_type": "code", "execution_count": 232, "metadata": {}, "outputs": [], "source": [ "VALID_MOVES = [[-1, 0],\n", " [-1, 1],\n", " [ 0, 1],\n", " [ 1, 1],\n", " [ 1, 0],\n", " [ 1,-1],\n", " [ 0,-1],\n", " [-1,-1]]\n", "\n", "\n", "def generate_ship_coordinates(x_start,y_start,direction,ship):\n", " ship_len = SHIPS[ship][1] - 1\n", " d_x = VALID_MOVES[direction][0]\n", " d_y = VALID_MOVES[direction][1]\n", "\n", " coords = [(x_start,y_start)]\n", " x_coord = x_start\n", " y_coord = y_start\n", " for i in range(ship_len):\n", " x_coord = x_coord + d_x\n", " y_coord = y_coord + d_y\n", " coords.append((x_coord,y_coord))\n", " return coords" ] }, { "cell_type": "code", "execution_count": 233, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "('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', 2) 1 1 3 [(1, 1), (2, 2)]\n", "('DESTROYER', 2) 8 3 6 [(8, 3), (8, 2)]\n" ] } ], "source": [ "\n", "for ship in range(len(SHIPS)):\n", " x,y = random_x_y()\n", " directions = get_possible_directions(x,y,ship)\n", " direction = random_direction(directions)\n", " coords = generate_ship_coordinates(x,y,direction,ship)\n", " 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, "metadata": {}, "outputs": [], "source": [] } ] }