if error:
print("error!")
error = True
- x = randint(N)
- y = randint(N)
+ x = randint(0, N - 1)
+ y = randint(0, N - 1)
if (x <= 0) or (x > N) or (y <= 0) or (y > N):
continue
dir_x = dir_y = 0
- o = randint(2)
+ o = randint(0, 1)
if o == 0:
dir_x = 1
if x + size - 1 > N:
continue
elif o == 1:
- dir_x = 0
+ dir_y = 1
if y + size - 1 > N:
continue
pos = []
else:
tab[pos.get("x")][pos.get("y")] = 'O'
else:
- tab[pos.get("x")][pos.get("y")] = 'S'
+ tab[pos.get("x")][pos.get("y")] = element
# check table status
def check_status(tab):
print("Where do you want to place a ship of", ship, "?")
pos = read_coordonate(ship, tab_human)
for p in pos:
- print(p)
put_in_table('S', p, tab_human)
# init table for machine
--- /dev/null
+# makefile
+
+MAKEFLAGS = -s
+PYTHON = python3
+SHELL = bash
+
+# Functions
+
+TITLE = echo -e "\033[0;1m$(strip $(1))\033[0;0m"
+PASS = echo -e "\033[1;32m$(strip $(1))\033[0;0m"
+WARN = echo -e "\033[1;33m$(strip $(1))\033[0;0m"
+FAIL = echo -e "\033[1;31m$(strip $(1))\033[0;0m"
+
+## Generic rules
+
+all:
+ echo "rules: purge tests"
+
+purge:
+ -rm -rf __pycache__
+
+tests: $(subst .py,,$(subst -,_,$(wildcard test-*.py)))
+
+## Specific rules
+
+test_%: test-%.py
+ IFS=$$'\n'; RC=0; \
+ for test in `cat $< | grep '^# test:' | sed 's/^[^:]*: //'`; do \
+ $(call TITLE, "=== $${test//\\/\\\\} ==="); \
+ eval $${test//%%TEST%%/$(PYTHON) $<}; \
+ [ $$? -eq 0 ] && $(call PASS, SUCCESS) || { $(call FAIL, FAILED); RC=1; }; \
+ done; \
+ if [ -z "$$test" ]; then \
+ $(call TITLE, "=== $< ==="); \
+ $(PYTHON) $<; \
+ [ $$? -eq 0 ] && $(call PASS, SUCCESS) || { $(call FAIL, FAILED); RC=1; }; \
+ fi; \
+ test "$$RC" -ne 1
+
+## Phony
+
+.PHONY: all clean count depends gcovs purge tests
+
+## Precious
+
+.PRECIOUS:
+
--- /dev/null
+#!/usr/bin/env python3
+# -*- coding: utf8 -*-
+
+from battleship import *
+
+init_table(tab_human)
+
+pos = read_coordonate(1, tab_human)
+put_in_table('S', pos[0], tab_human)
+
+pos = read_coordonate(4, tab_human)
+for p in pos:
+ put_in_table('L', p, tab_human)
+
+display(tab_human)
+
+# test: echo -e "1\n1\n4\n4\ne" | %%TEST%%
+# test: echo -e "1\n10\n2\n2\n4\n4\ne" | %%TEST%%
+# test: echo -e "1\n1\n8\n4\ns\n5\n5\ns" | %%TEST%%
+
+# vim: set ts=4 sw=4 et:
--- /dev/null
+#!/usr/bin/env python3
+# -*- coding: utf8 -*-
+
+from battleship import *
+
+init_table(tab_human)
+
+pos = generate_coordonate(1, tab_human)
+put_in_table('S', pos[0], tab_human)
+
+pos = generate_coordonate(4, tab_human)
+for p in pos:
+ print(p)
+ put_in_table('L', p, tab_human)
+
+display(tab_human)
+
+# vim: set ts=4 sw=4 et:
--- /dev/null
+#!/usr/bin/env python3
+# -*- coding: utf8 -*-
+
+from battleship import *
+
+init_table(tab_human)
+
+pos = {'x': 3, 'y': 6}
+put_in_table('S', pos, tab_human)
+
+put_in_table('B', {'x': 2, 'y': 8}, tab_human)
+if not(check_status(tab_human)):
+ print("still some ships are not skunk")
+display(tab_human)
+
+put_in_table('B', {'x': 3, 'y': 6}, tab_human)
+if check_status(tab_human):
+ print("you won")
+display(tab_human)
+
+# vim: set ts=4 sw=4 et: