Newer
Older
IrisServer / src / main / java / com / ntlab / irisserver / entities / Game.java
package com.ntlab.irisserver.entities;

import com.ntlab.irisserver.models.KeywordManager;

import java.util.*;

public class Game {

    private Map<Member, List<Integer>> assignments = new HashMap<>();//<Member, List<dno>>
    private Map<Integer, Drawing> drawingList = new HashMap<>();//<dno,Drawing>
    private Map<Integer, String> keywordList = new HashMap<>();//<dno, keyword>
    private Map<Integer, Cell> cellList = new HashMap<>();//<cno, Cell>

    private List<Integer> map = new ArrayList<>();//cno順にdnoを管理
    private List<String> color = new ArrayList<>();//cno順にr,g,b,dを管理
    private List<Turn> turnList = new ArrayList<>();

    private Turn nowTurn = null;//現在のターン
    private DrawingController drawingController = null;
    private Settings settings = null;

    public Game(Room room, String[] keywords) {

        room.setGame(this);
        this.settings = room.getSettings();
        drawingController = new DrawingController(room, room.getMembers(), this);

        List<Member> memberList = room.getMembers();
        int num = 16/memberList.size();//一人あたりが確実に採用されるイラスト枚数
        int cnt = 0;
        for(int i=0; i<memberList.size(); i++){
            List<Integer> dnoList = new ArrayList<>();//一人が描くイラストのdnoリスト
            for(int j=0; j<num; j++){
                dnoList.add(cnt);
                cnt++;
            }
            assignments.put(memberList.get(i), dnoList);
        }
        int remainder = 0;//残りの割り当て枚数
        if(16%memberList.size()!=0) remainder = ((num+1)* memberList.size()) - cnt;

        for(int i=0; i<remainder; i++){
            List<Integer> dnoList =  assignments.get(memberList.get(i));
            dnoList.add(cnt);
            cnt++;
            assignments.put(memberList.get(i), dnoList);
        }

        for(int i=0; i<16; i++) map.add(i);
        Collections.shuffle(map);//0~15のdnoをランダムにマップに割り振る
        System.out.println(map);
        //r:6 b:5 g:4 d:1
        for(int i=0; i<6; i++) color.add("r");
        for(int i=0; i<5; i++) color.add("b");
        for(int i=0; i<4; i++) color.add("g");
        color.add("d");
        Collections.shuffle(color);//r,b,g,dをランダムにマップに割り振る

        List<String> randKeys = Arrays.asList(keywords);//コンストラクタの引数で受け取ったキーワードをリストに変換
        Collections.shuffle(randKeys);//キーワードをシャッフル

        //Cellのインスタンス作成
        for(int i=0; i<16; i++){
            Cell c = new Cell();
            cellList.put(i, c);
            c.setCno(i);
            c.setColor(color.get(i));
            c.setDno(map.get(i));

            //今回のゲームで使用するキーワードを追加
            keywordList.put(i, randKeys.get(i));//(dno, キーワード)
        }
    }

    public DrawingController getDrawingController() {
        return drawingController;
    }

    public Drawing getDrawing(int dno) {
        Drawing d = drawingList.get(dno);
        return d;
    }

    public Map<Integer, Drawing> getDrawings(){
        return drawingList;
    }

    public Drawing putDrawing(int dno, Drawing drawing) {
        Drawing d = drawingList.put(dno, drawing);
        drawingController.update();
        return d;
    }

    public String getKeyword(int dno) {
        String keyword = keywordList.get(dno);
        return keyword;
    }

    public List<String> getKeywords() {
        List<String> keywords = new ArrayList<>();
        for(int i=0; i<16; i++){
            keywords.add(keywordList.get(map.get(i)));
        }
        return keywords;
    }

    public List<Boolean> getOpens(){
        List<Boolean> opens= new ArrayList<>();
        for(int i=0; i<16; i++){
            Cell cell = cellList.get(i);
            boolean isOpen = cell.getIsOpen();
            opens.add(isOpen);
        }
        return opens;
    }

    public Map<Member, List<Integer>> getAssignments(){
        return assignments;
    }

    public List<Integer> getMap(){
        return map;
    }
    public List<String> getColor(){
        return color;
    }

    public Settings getSettings(){
        return this.settings;
    }

    public void createTurn(){
        nowTurn = new Turn();
        if(turnList.size()%2==0){//偶数ターンなら青い色,奇数ターンは赤色
            nowTurn.setTeam("b");
        }else{
            nowTurn.setTeam("r");
        }
        this.turnList.add(nowTurn);
    }

    public Turn getTurn(Integer num){
        return turnList.get(num);
    }

    public Integer getNowTurn(){//現在何ターン目か取得
        return turnList.size()-1;
    }

    public Cell getCell(Integer cno){
        return cellList.get(cno);
    }
}