{ "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.7.3 64-bit ('python': venv)", "metadata": { "interpreter": { "hash": "d99f099520c2505e9f745916a84fcd3ec6a6f02f993bb8e12b586a85fced6daa" } } } }, "nbformat": 4, "nbformat_minor": 2, "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import random" ] }, { "cell_type": "code", "execution_count": 2, "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": 3, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "5 2\n" ] } ], "source": [ "x,y = random_x_y()\n", "print (x,y)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def random_direction(valid_directions):\n", " idx = random.ran range(len(valid_directions))\n", " return valid_directions[idx]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1\n" ] } ], "source": [ "print(random_direction([0,1,2]))" ] }, { "cell_type": "code", "execution_count": 6, "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": 7, "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": 10, "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": 14, "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', 2) 5 9 6 [(5, 9), (5, 8)]\n('DESTROYER', 2) 3 4 3 [(3, 4), (4, 5)]\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": null, "metadata": {}, "outputs": [], "source": [] } ] }