def _save_ai_point_board(self):
     try:
         with open(join_(path_gamedata_pointmaps_, "%s_%s_%s.txt" %
                 (self.p_number, _game_number, id(self.playerType))), "a+") as file:
             file.write("{'turn_%s': %s}\n" % (turns, str(self._point_map)))
     except FileNotFoundError:
         open(join_(path_gamedata_pointmaps_, "%s_%s_%s.txt" %
                    (self.p_number, _game_number, id(self.playerType))), "w+").close()
         self._save_ai_point_board()
 def save_data(self):
     """
     This function is used to save game data after a win.
     """
     global start
     global turns
     if Save_Stats:
         with open(join_(path_gamedata_, "battleship_stats.txt"), "a+") as file:
             file.write(out_put_data[25] % (self.player, (self.hits+self.misses), self.hits,
                                            (self.hits / (self.hits+self.misses)), turns,
                                            self.difficulty if self.difficulty else "Player"))
         with open(join_(path_gamedata_, "battleship_time.txt"), "a+") as file:
             file.write("%s\n" % (time.time() - start))
     turns = 0
def _create_master_game_number(override=False):
    global _game_number
    try:
        with open(join_(path_gamedata_, "GameNumber.txt"), "r+") as file:
            file.seek(0)
            game_number = file.readline()
            _game_number = int(game_number[12:]) + 1 + (override if not override else 0)
        with open(join_(path_gamedata_, "GameNumber.txt"), "w") as file:
            file.seek(0)
            file.write("Game Number: %s" % str(_game_number))
    except FileNotFoundError:
        with open(join_(path_gamedata_, "GameNumber.txt"), "w") as file:
            file.seek(0)
            _game_number = 1
            file.write("Game Number: %s" % str(_game_number))
Example #4
0
__author__ = "Charles Engen"

from os import getcwd
from os.path import join as join_

_path = getcwd()
_path_gamedata = join_(_path, "GameData")

scores = []

with open(join_(_path_gamedata, "battleship_time.txt"), "r") as file:
    for line in file:
        scores.append(eval(line))


def get_average_accuracy(score_obj):
    return sum(score_obj) / len(score_obj)


print(get_average_accuracy(scores))
 def load_point_map(self, file):
     with open(join_(_path_gamedata_pointmaps, file)) as file:
         temp = []
         for line in file:
             temp.append(eval(line))
         return temp
__author__ = 'Charles Engen'

import tkinter
from os.path import join as join_
from os import getcwd
from os import listdir
import re

_path = getcwd()
_path_gamedata = join_(_path, "GameData")
_path_gamedata_pointmaps = join_(_path_gamedata, "point_maps")

point_map = {(x, y): None for x in range(1, 11) for y in range(1, 11)}


class PointMaps(object):

    def __init__(self):
        self.point_map_files = listdir(_path_gamedata_pointmaps)
        self.load_base_number = 1
        self.point_maps = dict()

    def load_point_map(self, file):
        with open(join_(_path_gamedata_pointmaps, file)) as file:
            temp = []
            for line in file:
                temp.append(eval(line))
            return temp

    def lowest_game_number(self):
        min_val = 0
__author__ = 'Charles Engen'

from os import getcwd
from os.path import join as join_

_path = getcwd()
_path_gamedata = join_(_path, "GameData")

scores = []

with open(join_(_path_gamedata, "battleship_stats.txt"), 'r') as file:
    for line in file:
        scores.append(eval(line))


def get_average_accuracy(score_obj):
    total = 0
    shots_win = 0
    min_shot = 100
    for score in score_obj:
        total += score['Accuracy']
        shots_win += score['Shots']
        if score['Shots'] < min_shot:
            min_shot = score['Shots']
    return "Average Accuracy: %0.2f \nShots to Win: %0.2f \nTotal Games: %s\n Best Game: %s" % \
           ((total / len(score_obj) * 100), (shots_win / len(score_obj)), len(score_obj), min_shot)

print(get_average_accuracy(scores))
def _create_dirs():
    if not path.exists(path_gamedata_):
        makedirs(join_(path_, "GameData"))
    if not path.exists(path_gamedata_pointmaps_):
        makedirs(join_(path_gamedata_, "point_maps"))
from os.path import join as join_
from os import getcwd
from os import makedirs
from os import path
import time
import cProfile

Display = True
Difficulty = 2
start = 0
Save_Stats = True
Save_Point_Maps = True
turns = 0
_game_number = None
path_ = getcwd()
path_gamedata_ = join_(path_, "GameData")
path_gamedata_pointmaps_ = join_(path_gamedata_, "point_maps")

board_size = 10

out_put_data = {
    0: "Player %s Are you 'Man' or 'Machine'?",
    1: "You failed to enter a valid entry.",
    2: "What is your name?",
    3: "Pick a number 1-10",
    4: "You have already fired here at [%s, %s]",
    5: "At [%s, %s] was nothing!",
    6: 'You have hit the something at [%s, %s]',
    7: "You tried and failed to attack, try again.",
    8: "You sunk %s!",
    9: "You are placing the %s",