Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using UnityEngine;
- using System.Collections;
- using System;
- using System.IO;
- namespace NeuralNetwork
- {
- public enum ActivityEnum
- {
- STAY,
- GO_LEFT,
- GO_RIGHT,
- DROP_BOMB,
- GO_UP,
- GO_DOWN
- }
- public class Ai : MonoBehaviour
- {
- public GlobalStateManager globalManager;
- public int counterWait = 0;
- System.Random randomActivity;
- string tmpString;
- public List<string> movementListString = new List<string>();
- static FileStream fileStream = new FileStream("answers.txt", FileMode.Append, FileAccess.Write);
- public int playerNumber = 3;
- public float moveSpeed = 5f;
- public bool canDropBombs = true;
- public bool canMove = true;
- public bool dead = false;
- private int bombs = 2;
- private ActivityEnum activity;
- public GameObject bombPrefab;
- private Rigidbody rigidBody;
- private Transform myTransform;
- private Animator animator;
- Vector3 position = new Vector3();
- void Start()
- {
- rigidBody = GetComponent<Rigidbody>();
- myTransform = transform;
- animator = myTransform.Find("PlayerModel").GetComponent<Animator>();
- setRandomPosition();
- position = transform.position;
- }
- void Update()
- {
- if(counterWait == 20)
- {
- counterWait = 0;
- activity = randomEnum();
- UpdateMovement();
- fillStringAnswer(activity);
- movementListString.Add(tmpString);
- if (globalManager.deadPlayerNumber == 1 || globalManager.deadPlayerNumber == 2)
- {
- saveIteration(ref movementListString);
- }
- }
- else
- {
- counterWait++;
- }
- }
- private void saveIteration(ref List<string> list)
- {
- StreamWriter fileWriter = new StreamWriter(fileStream);
- for (int i = 0; i < list.Count; i++)
- {
- fileWriter.Write(list[i]);
- fileWriter.Write("\r\n");
- }
- fileWriter.Write("\r\n");
- fileWriter.Close();
- }
- private void fillStringAnswer( ActivityEnum activity)
- {
- if (activity == ActivityEnum.DROP_BOMB)
- {
- tmpString = "000100";
- }
- else if (activity == ActivityEnum.GO_DOWN)
- {
- tmpString = "000001";
- }
- else if (activity == ActivityEnum.GO_LEFT)
- {
- tmpString = "010000";
- }
- else if (activity == ActivityEnum.GO_RIGHT)
- {
- tmpString = "001000";
- }
- else if (activity == ActivityEnum.GO_UP)
- {
- tmpString = "000010";
- }
- else if (activity == ActivityEnum.STAY)
- {
- tmpString = "100000";
- }
- }
- private ActivityEnum randomEnum()
- {
- ActivityEnum enumerator = new ActivityEnum();
- randomActivity = new System.Random();
- randValue = randomActivity.Next(6, 99);
- randValue = randValue % 6;
- if (randValue == 0)
- {
- enumerator = ActivityEnum.STAY;
- }
- if (randValue == 1)
- {
- enumerator = ActivityEnum.GO_LEFT;
- }
- if (randValue == 2)
- {
- enumerator = ActivityEnum.DROP_BOMB;
- }
- if (randValue == 3)
- {
- enumerator = ActivityEnum.GO_UP;
- }
- if (randValue == 4)
- {
- enumerator = ActivityEnum.GO_DOWN;
- }
- if (randValue == 5)
- {
- enumerator = ActivityEnum.GO_RIGHT;
- }
- return enumerator;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement