public static Move makeMove(Board board) { //If not the first move if (!board.isFirstMove()) { ScoreMove val = minimax(board, MAX_DEPTH, int.MinValue, int.MaxValue, true); Debug.Log(val.score); if (val.move != null) { return(val.move); } } Debug.Log("Returning random Move"); HashSet <Move> set = getAvailableMoves(board); int randNum = Random.Range(0, set.Count); int i = 0; foreach (Move move in set) { if (i == randNum) { // Debug.Log("Move from AI"+move.ToString()); return(move); } i++; } throw new UnityException("AHH BADDDDD"); }
private void Awake() { m_appState = GameObject.FindObjectOfType <AppState>(); m_udpHelper = GameObject.FindObjectOfType <UDPHelper>(); m_scoreMove = GameObject.FindObjectOfType <ScoreMove>(); m_keyManager = GameObject.FindObjectOfType <KeyManager>(); m_scoreManage = GameObject.FindObjectOfType <scoremanage>(); }
// Use this for initialization void Start() { m_scoreMove = GameObject.FindObjectOfType <ScoreMove>(); m_appState = GameObject.FindObjectOfType <AppState>(); m_udpHelper = GameObject.FindObjectOfType <UDPHelper>(); var server = GetComponent <uOscServer>(); server.onDataReceived.AddListener(OnDataReceived); }
/** * Minimax with alpha beta pruning. * Returns hueristic * */ static ScoreMove minimax(Board board, int depth, int alpha, int beta, bool maximize) { if (depth == 0) //or terminal node (game over) //BoardHelper.getInstance().checkWin(x,y,board) { return(new ScoreMove(hueristic(board), null)); } //update turn if (depth != MAX_DEPTH) { board.updateTurn(); } if (maximize) { ScoreMove bestValue = new ScoreMove(int.MinValue, null); foreach (Move move in getAvailableMoves(board)) { Vector2[] clicks = move.getClicks(); BoardHelper helper = BoardHelper.getInstance(); Board tempBoard = new Board(board); Vector2 lastMove; if (clicks.Length == 2) { tempBoard = helper.simulateMove(tempBoard, clicks[0]); tempBoard = helper.simulateMove(tempBoard, clicks[1]); lastMove = clicks[1]; } else //it is length 1 { tempBoard = helper.simulateMove(tempBoard, clicks[0]); lastMove = clicks[0]; } if (helper.checkWin(lastMove, tempBoard) == COMPUTER_NUMBER) { Debug.Log("Max: " + move); return(new ScoreMove(WIN_HUERISTIC, move)); } ScoreMove val = minimax(tempBoard, depth - 1, alpha, beta, !maximize); if (bestValue.score < val.score) { bestValue = val; bestValue.move = move; } alpha = Mathf.Max(alpha, val.score); if (beta <= alpha) { // break; } } return(bestValue); } else { ScoreMove bestValue = new ScoreMove(int.MaxValue, null); foreach (Move move in getAvailableMoves(board)) { Vector2[] clicks = move.getClicks(); BoardHelper helper = BoardHelper.getInstance(); Board tempBoard = new Board(board); Vector2 lastMove; if (clicks.Length == 2) { tempBoard = helper.simulateMove(tempBoard, clicks[0]); tempBoard = helper.simulateMove(tempBoard, clicks[1]); lastMove = clicks[1]; } else { tempBoard = helper.simulateMove(tempBoard, clicks[0]); lastMove = clicks[0]; } if (helper.checkWin(lastMove, tempBoard) == PLAYER_NUMBER) { Debug.Log("Min: " + move); return(new ScoreMove(-1 * WIN_HUERISTIC, move)); } //update turn //tempBoard.updateTurn(); ScoreMove val = minimax(tempBoard, depth - 1, alpha, beta, !maximize); if (bestValue.score > val.score) { bestValue = val; bestValue.move = move; } beta = Mathf.Min(beta, val.score); if (beta <= alpha) { // break; } } return(bestValue); } }
public static void Read_midi(byte[] midi_byte, GameObject obj, int tick, int freq, List <int> fingers, bool multifinger_mode, AppState m_appState, UDPHelper m_udpHelper, ScoreMove m_scoreMove, KeyManager m_keyManager, scoremanage m_scoremanage, GameObject BarObj) { Stack <Vector3> notes = new Stack <Vector3>(); notes.Push(new Vector3(0, -100, 0.01f)); int[] noteOn_time = new int[128]; //各キーのnote-onの時間 int tick_time = 0; //delta_time累積値 bool in_MTrk = false; float min_noteLength = 4 / (float)freq; float scale = 50.0f; SortedSet <int> keySet = new SortedSet <int>(); List <KeyManager.KeyInfo> ListKeyInfo = new List <KeyManager.KeyInfo>(); List <Color> FingerColor = new List <Color>() { new Color(1, 0.5f, 0.5f, 1), new Color(1, 1, 0.5f, 1), new Color(0.5f, 1, 0.5f, 1), new Color(0.5f, 1, 1, 1), new Color(0.5f, 0.5f, 1, 1) }; GameObject origin, pre_note = Instantiate(obj); origin = GameObject.Find("ScoreOrigin"); pre_note.transform.SetParent(origin.transform); pre_note.GetComponent <RectTransform>().localPosition = new Vector3(0.0f, -100f, 0.0f); m_keyManager.KeyInfoInit(); for (int i = 4; i < midi_byte.Length; i++) { //トラックチャンク初め(4D 54 72 6B)と終わり(00 FF 2F 00) if (midi_byte[i] == 77 && midi_byte[i + 1] == 84 && midi_byte[i + 2] == 114 && midi_byte[i + 3] == 107) { in_MTrk = true; i += 8; //MTrkとlengthを飛ばす } else if (midi_byte[i] == 0 && midi_byte[i + 1] == 255 && midi_byte[i + 2] == 47 && midi_byte[i + 3] == 0) { in_MTrk = false; break; } if (in_MTrk) { int[] tuple_time_size = Variable2int(midi_byte, i); //(delta_time, data size of delta_time) tick_time += tuple_time_size[0]; i += tuple_time_size[1]; if (midi_byte[i] == 144) //90: note on { int key = midi_byte[i + 1]; noteOn_time[key] = tick_time; //noteの先頭の位置 i += 2; } else if (midi_byte[i] == 128) //80: note off { int key = midi_byte[i + 1]; keySet.Add(key); KeyManager.KeyInfo m_keyInfo; m_keyInfo.key = key; m_keyInfo.OnTime = noteOn_time[key] / (float)tick; m_keyInfo.OffTime = tick_time / (float)tick; m_keyInfo.tag = 0; ListKeyInfo.Add(m_keyInfo); i += 2; } else if (midi_byte[i] == 192) //C0 { i += 1; } else if (midi_byte[i] == 176 || midi_byte[i] == 224) //B0, E0 { i += 2; } else if (midi_byte[i] == 255) //FF { i += midi_byte[i + 2] + 2; } } } //Assign notes to five buttons int[] keyMap = new int[128]; //Debug.Log("fingers: " + fingers.Count); int assignNum = fingers[0], ind = 0; foreach (var key in keySet) { keyMap[key] = assignNum; ind = (ind + 1) % fingers.Count; assignNum = fingers[ind]; } foreach (var _keyinfo in ListKeyInfo) { // Spawn the note object float note_height = _keyinfo.OffTime - _keyinfo.OnTime; Vector3 pos = new Vector3((assignNum - 2) * fingerInterval, _keyinfo.OnTime, 0); Vector2 siz = new Vector2(noteWidth, Mathf.Max(note_height, (float)(1.0 / 8.0))); GameObject note = Instantiate(obj); note.transform.SetParent(origin.transform); note.GetComponent <RectTransform>().sizeDelta = siz; note.GetComponent <RawImage>().color = FingerColor[assignNum]; float pre_y = pre_note.GetComponent <RectTransform>().localPosition.y; float pre_size = pre_note.GetComponent <RectTransform>().sizeDelta.y; Vector3 SendVec; //(fin_num, y_position, Height) if (pos.y - pre_y < min_noteLength || (assignNum == keyMap[_keyinfo.key] && pos.y - (pre_y + pre_size) < 1.0 / 8.0)) { //結合 note_height = pos.y + siz.y - pre_y; note.GetComponent <RectTransform>().localPosition = new Vector3(pos.x, pre_y, 0); note.GetComponent <RectTransform>().sizeDelta = new Vector3(noteWidth, note_height); Destroy(pre_note); notes.Pop(); SendVec = new Vector3(assignNum, pre_y / scale, note_height / scale); notes.Push(SendVec); } else { assignNum = keyMap[_keyinfo.key]; note.GetComponent <RectTransform>().localPosition = pos = new Vector3((assignNum - 2) * fingerInterval, _keyinfo.OnTime, 0);; note.GetComponent <RawImage>().color = FingerColor[assignNum]; SendVec = new Vector3(assignNum, pos.y / scale, note_height / scale); notes.Push(SendVec); } /* * if (multifinger_mode) * { * Clone(note, fingers); * } */ m_keyManager.KeyInfoList[assignNum].Add(_keyinfo); note.GetComponent <BoxCollider>().center = new Vector3(0, note_height / 2.0f, 0); note.GetComponent <BoxCollider>().size = new Vector3(0.5f, note_height, 0.2f); pre_note = note; } for (int i = 0; i < m_keyManager.KeyInfoList.Count; i++) { m_keyManager.KeyInfoList[i].Sort((a, b) => FloatSort(a.OnTime, b.OnTime)); } m_keyManager.KeyInit(); int LastTime = tick_time / tick; if (tick_time % tick != 0) { LastTime++; } m_scoreMove.MusicLength = LastTime; for (int i = 0; i < LastTime; i++) { GameObject barInstance = Instantiate(BarObj); barInstance.transform.SetParent(origin.transform); barInstance.GetComponent <RectTransform>().localPosition = new Vector3(0, i, 0); barInstance.transform.SetAsFirstSibling(); } NetOutMessage outMessage = new NetOutMessage(); outMessage.WriteInt32((int)MessageType.Command.Control); outMessage.WriteInt32((int)MessageType.ControlType.SpawnNote); int NumberofNotes = notes.Count; m_scoremanage.NofNote = notes.Count - 1; outMessage.WriteInt32(NumberofNotes); //Debug.Log("Number of notes is " + (NumberofNotes - 1)); while (notes.Count > 0) { outMessage.WriteVector3(notes.Peek()); notes.Pop(); } outMessage.WriteInt32(LastTime); m_udpHelper.Send(outMessage, m_appState.HololensIP, Constants.NETWORK_PORT); }