Advertisement
Guest User

Untitled

a guest
May 11th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using UnityEngine;
  7. using System.Collections;
  8. using System;
  9. using System.IO;
  10.  
  11. namespace NeuralNetwork
  12. {
  13.  
  14.     public enum ActivityEnum
  15.     {
  16.         STAY,
  17.         GO_LEFT,
  18.         GO_RIGHT,
  19.         DROP_BOMB,
  20.         GO_UP,
  21.         GO_DOWN
  22.        
  23.     }
  24.  
  25.     public class Ai : MonoBehaviour
  26.     {
  27.         public GlobalStateManager globalManager;
  28.  
  29.         public int counterWait = 0;
  30.         System.Random randomActivity;
  31.  
  32.  
  33.         string tmpString;
  34.         public List<string> movementListString = new List<string>();
  35.         static FileStream fileStream = new FileStream("answers.txt", FileMode.Append, FileAccess.Write);
  36.  
  37.  
  38.  
  39.  
  40.         public int playerNumber = 3;
  41.  
  42.         public float moveSpeed = 5f;
  43.         public bool canDropBombs = true;
  44.         public bool canMove = true;
  45.         public bool dead = false;
  46.  
  47.  
  48.         private int bombs = 2;
  49.         private ActivityEnum activity;
  50.  
  51.         public GameObject bombPrefab;
  52.  
  53.         private Rigidbody rigidBody;
  54.         private Transform myTransform;
  55.         private Animator animator;
  56.  
  57.         Vector3 position = new Vector3();
  58.  
  59.  
  60.         void Start()
  61.         {
  62.             rigidBody = GetComponent<Rigidbody>();
  63.             myTransform = transform;
  64.             animator = myTransform.Find("PlayerModel").GetComponent<Animator>();
  65.             setRandomPosition();
  66.             position = transform.position;          
  67.         }
  68.  
  69.         void Update()
  70.         {
  71.             if(counterWait == 20)
  72.             {
  73.                 counterWait = 0;
  74.                 activity = randomEnum();
  75.                 UpdateMovement();
  76.  
  77.                 fillStringAnswer(activity);
  78.                 movementListString.Add(tmpString);
  79.                 if (globalManager.deadPlayerNumber == 1 || globalManager.deadPlayerNumber == 2)
  80.                 {
  81.                     saveIteration(ref movementListString);
  82.                 }
  83.  
  84.             }
  85.             else
  86.             {
  87.                 counterWait++;
  88.             }            
  89.         }
  90.  
  91.  
  92.  
  93.         private void saveIteration(ref List<string> list)
  94.         {
  95.             StreamWriter fileWriter = new StreamWriter(fileStream);
  96.  
  97.                 for (int i = 0; i < list.Count; i++)
  98.                 {
  99.                     fileWriter.Write(list[i]);
  100.                     fileWriter.Write("\r\n");
  101.                 }
  102.                 fileWriter.Write("\r\n");
  103.  
  104.             fileWriter.Close();
  105.         }
  106.  
  107.         private void fillStringAnswer( ActivityEnum activity)
  108.         {
  109.             if (activity == ActivityEnum.DROP_BOMB)
  110.             {
  111.                 tmpString = "000100";
  112.             }
  113.             else if (activity == ActivityEnum.GO_DOWN)
  114.             {
  115.                 tmpString = "000001";
  116.             }
  117.             else if (activity == ActivityEnum.GO_LEFT)
  118.             {
  119.                 tmpString = "010000";
  120.             }
  121.             else if (activity == ActivityEnum.GO_RIGHT)
  122.             {
  123.                 tmpString = "001000";
  124.             }
  125.             else if (activity == ActivityEnum.GO_UP)
  126.             {
  127.                 tmpString = "000010";
  128.             }
  129.             else if (activity == ActivityEnum.STAY)
  130.             {
  131.                 tmpString = "100000";
  132.             }
  133.         }
  134.  
  135.  
  136.         private ActivityEnum randomEnum()
  137.         {
  138.             ActivityEnum enumerator = new ActivityEnum();
  139.             randomActivity = new System.Random();
  140.             randValue = randomActivity.Next(6, 99);
  141.             randValue = randValue % 6;
  142.  
  143.             if (randValue == 0)
  144.             {
  145.                 enumerator = ActivityEnum.STAY;
  146.             }
  147.             if (randValue == 1)
  148.             {
  149.                 enumerator = ActivityEnum.GO_LEFT;
  150.             }
  151.             if (randValue == 2)
  152.             {
  153.                 enumerator = ActivityEnum.DROP_BOMB;
  154.             }
  155.             if (randValue == 3)
  156.             {
  157.                 enumerator = ActivityEnum.GO_UP;
  158.             }
  159.             if (randValue == 4)
  160.             {
  161.                 enumerator = ActivityEnum.GO_DOWN;
  162.             }
  163.             if (randValue == 5)
  164.             {
  165.                 enumerator = ActivityEnum.GO_RIGHT;
  166.             }
  167.            
  168.             return enumerator;
  169.         }
  170.  
  171.  
  172.  
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement