public GamePlayer(Player player) { this.grid = new BattleshipGrid(10, 10); this.Player = player; this.HitPoints = 17; this.HasPlacedBoats = false; }
bool checkFree(int x, int y, BattleshipGrid g, bool orientation) { if (!orientation) { foreach (Block b in g.blocks) { if (b.indexX >= x && b.indexX < x + numberofblocks && b.indexY == y) { if (b.filled) { return(false); } } } } else { foreach (Block b in g.blocks) { if (b.indexY >= y && b.indexY < y + numberofblocks && b.indexX == x) { if (b.filled) { return(false); } } } } return(true); }
public SelectionGrid(Game game, bool isFriendly, Vector3D position) : base(game) { this.Position = position; this.battleshipGrid = new BattleshipGrid(1, 10, 10, new Point3D(this.Position.X, this.Position.Y, this.Position.Z)); //this.gridObject = new GridObject(1, 0, 0); this.index = new Point(0, 0); this.isFriendly = isFriendly; this.GeometryModel = ModelUtil.ConvertToGeometryModel3D(new OBJModelLoader().LoadModel(Asset.GridPlaneModel)); this.Material = new DiffuseMaterial(new ImageBrush(new BitmapImage(new Uri(Asset.GridImage, UriKind.Absolute)))); this.Marker = new GameObject(game); this.Marker.Position = new Vector3D(this.Position.X - 4.5, this.Position.Y, this.Position.Z - 4.5); this.Marker.GeometryModel = ModelUtil.ConvertToGeometryModel3D(new OBJModelLoader().LoadModel(Asset.HighlighterModel)); this.Marker.Material = new DiffuseMaterial(Brushes.Blue); this.Ship = new Ship(game, 1); ActivateControls(); SetActive(true); this.mediaPlayer = new MediaPlayer(); }
public void place(int x, int y, bool orientation, BattleshipGrid grid) { //does not allow me to place a ship twice if (!placed) { if (!orientation) { //horizontal --* if (x + (numberofblocks - 1) <= 10) { //should fit horizontally, if no ships in the way if (checkFree(x, y, grid, false)) { foreach (Block b in grid.blocks) { if (b.indexX >= x && b.indexX < x + numberofblocks && b.indexY == y) { b.toptile.GetComponent <SpriteRenderer>().color = Color.grey; b.bottomtile.GetComponent <SpriteRenderer>().color = this.backColor; b.filled = true; } } placed = true; _x = x; _y = y; } } } else { //* vertical vertical = true; if (y + (numberofblocks - 1) <= 10) { if (checkFree(x, y, grid, true)) { foreach (Block b in grid.blocks) { //should fit vertically, if no ships in the way if (b.indexY >= y && b.indexY < y + numberofblocks && b.indexX == x) { b.toptile.GetComponent <SpriteRenderer>().color = Color.grey; b.bottomtile.GetComponent <SpriteRenderer>().color = this.backColor; b.filled = true; } } placed = true; _x = x; _y = y; } } } } }
BattleshipGrid GenerateGrid(GameObject parentObject) { int rowcounter = 0; int columncounter = 0; int lettercounter = 0; BattleshipGrid grid = new BattleshipGrid(); for (float ycoord = -4.5f; ycoord <= 4.5f; ycoord++) { //for each row rowcounter++; rowL = Instantiate(rowLabel, new Vector3(-5.5f, ycoord), Quaternion.identity); rowL.GetComponentInChildren <Text>().text = rowcounter.ToString(); rowL.transform.SetParent(parentObject.transform); //rowL.transform.GetChild(0).transform.position = new Vector3(-2f, ycoord)); for (float xcoord = -4.5f; xcoord <= 4.5f; xcoord++) { //first row at the top if (ycoord == 4.5f) { rowL = Instantiate(rowLabel, new Vector3(xcoord, 5.5f), Quaternion.identity); rowL.GetComponentInChildren <Text>().text = letters[lettercounter]; rowL.transform.SetParent(parentObject.transform); lettercounter++; } columncounter++; Block b = new Block(); b.bottomtile = Instantiate(sq, new Vector3(xcoord, ycoord), Quaternion.identity); b.toptile = Instantiate(sq, new Vector3(xcoord, ycoord), Quaternion.identity); b.toptile.transform.localScale = new Vector3(0.8f, 0.8f); b.toptile.name = "TopTile"; b.toptile.AddComponent <BoxCollider2D>(); b.toptile.GetComponent <BoxCollider2D>().isTrigger = true; b.bottomtile.GetComponent <SpriteRenderer>().color = Color.black; b.toptile.transform.SetParent(parentObject.transform); b.bottomtile.transform.SetParent(parentObject.transform); b.bottomtile.name = "BottomTile"; //setting the indexes of the blocks b.indexX = columncounter; b.indexY = rowcounter; grid.blocks.Add(b); } columncounter = 0; } grid.parent = parentObject; return(grid); }
public bool checkHit(Shot s, BattleshipGrid g) { //linq is quite cool. Are there any hits that have the same x and y in the list of hits?? if (!hits.Any(hit => (hit.x == s.x && hit.y == s.y))) { //I want hit to return true if the place where the shot is is filled if (checkHit(s.x, s.y, this.vertical)) { hits.Add(s); return(true); } else { return(false); } } else { Debug.Log("hit already!"); return(false); } }
public void Setup() { _battleship = new BattleshipGrid(new Random(1)); }
// Start is called before the first frame update void Start() { sq = Resources.Load <GameObject>("Prefabs/Square"); rowLabel = Resources.Load <GameObject>("Prefabs/TextPrefab"); buttonPrefab = Resources.Load <GameObject>("Prefabs/myButton"); timerText = rowLabel; allships = new Ship[5]; Ship carrier = new Ship(5); Ship battleship = new Ship(4); Ship cruiser = new Ship(3); Ship submarine = new Ship(3); Ship destroyer = new Ship(2); allships[4] = carrier; allships[3] = battleship; allships[2] = cruiser; allships[1] = submarine; allships[0] = destroyer; GameObject anchor = new GameObject("playergrid"); GameObject anchor2 = new GameObject("enemygrid"); GameObject anchor3 = new GameObject("shipselectiongrid"); //draw player grid playerGrid = GenerateGrid(anchor); playerGrid.parent.transform.position = new Vector3(-10f, -10f); playerGrid.parent.transform.localScale = new Vector3(1.5f, 1.5f); playerGrid.makeClickable(); //ship selection grid Button carrierButton = createWorldButton("Carrier", anchor3, new Vector3(0f, 1f)); carrierButton.onClick.AddListener( () => { Debug.Log("Carrier"); //carrierButton.enabled = false; currentlySelectedShip = allships[4]; currentbutton = carrierButton; } ); Button battleshipButton = createWorldButton("Battleship", anchor3, new Vector3(0f, -2f)); battleshipButton.onClick.AddListener( () => { Debug.Log("Battleship"); currentlySelectedShip = allships[3]; currentbutton = battleshipButton; } ); Button submarineButton = createWorldButton("Submarine", anchor3, new Vector3(0f, -5f)); submarineButton.onClick.AddListener( () => { Debug.Log("Submarine"); allships[2].setBackColor(Color.blue); currentlySelectedShip = allships[2]; currentbutton = submarineButton; } ); Button cruiserButton = createWorldButton("Cruiser", anchor3, new Vector3(0f, -8f)); cruiserButton.onClick.AddListener( () => { Debug.Log("Cruiser"); currentlySelectedShip = allships[1]; currentbutton = cruiserButton; } ); Button destroyerButton = createWorldButton("Destroyer", anchor3, new Vector3(0f, -11f)); destroyerButton.onClick.AddListener( () => { Debug.Log("Destroyer"); currentlySelectedShip = allships[0]; currentbutton = destroyerButton; } ); Button removeships = createWorldButton("Remove Fleet", anchor3, new Vector3(0f, -14f)); removeships.onClick.AddListener( () => { Debug.Log("Color Change Active"); foreach (Block b in playerGrid.blocks) { b.bottomtile.GetComponent <SpriteRenderer>().color = Color.black; b.toptile.GetComponent <SpriteRenderer>().color = Color.white; carrierButton.interactable = true; carrier.placed = false; battleshipButton.interactable = true; battleship.placed = false; submarineButton.interactable = true; submarine.placed = false; cruiserButton.interactable = true; cruiser.placed = false; destroyerButton.interactable = true; destroyer.placed = false; b.filled = false; } } ); anchor3.transform.position = new Vector3(10f, -4f); enemyGrid = GenerateGrid(anchor2); enemyGrid.parent.transform.position = new Vector3(10f, 10f); enemyGrid.parent.transform.localScale = new Vector3(1.5f, 1.5f); enemyGrid.makeClickableEnemy(); //at the moment, theTimer contains the word 'test' theTimer = Instantiate(timerText, new Vector3(-18f, 19f), Quaternion.identity); //gameSession, which is declared a bit further up. session = new gameSession(allships); Debug.Log("Hope this runs " + session.gameStarted); if (session.gameStarted) { removeships.interactable = false; } StartCoroutine(myTurn()); }
// Start is called before the first frame update void Start() { //template for a square sq = Resources.Load <GameObject>("Prefabs/Square"); //template for the labels and text that there is on screen rowLabel = Resources.Load <GameObject>("Prefabs/TextPrefab"); //template for the buttons that are loaded on screen buttonPrefab = Resources.Load <GameObject>("Prefabs/myButton"); //add the firebase script to the main camera Camera.main.gameObject.AddComponent <FirebaseScript>(); dbScript = Camera.main.GetComponent <FirebaseScript>(); //made a copy of rowlabel in the variable timertext timerText = rowLabel; allships = new Ship[5]; Ship carrier = new Ship("Carrier", 5); Ship battleship = new Ship("Battleship", 4); Ship cruiser = new Ship("Cruiser", 3); Ship submarine = new Ship("Submarine", 3); Ship destroyer = new Ship("Destroyer", 2); allships[4] = carrier; allships[3] = battleship; allships[2] = submarine; allships[1] = cruiser; allships[0] = destroyer; GameObject anchor = new GameObject("playergrid"); GameObject anchor2 = new GameObject("enemygrid"); GameObject anchor3 = new GameObject("shipselectiongrid"); //draw player grid playerGrid = GenerateGrid(anchor); anchor.transform.position = new Vector3(-10f, -10f); anchor.transform.localScale = new Vector3(1.5f, 1.5f); playerGrid.makeClickable(); //ship selection grid Button carrierButton = createWorldButton("Carrier", anchor3, new Vector3(0f, 0f)); carrierButton.onClick.AddListener( () => { Debug.Log("Carrier"); //carrierButton.enabled = false; currentlySelectedShip = allships[4]; } ); Button battleshipButton = createWorldButton("Battleship", anchor3, new Vector3(0f, -3f)); battleshipButton.onClick.AddListener( () => { Debug.Log("Battleship"); currentlySelectedShip = allships[3]; } ); Button submarineButton = createWorldButton("Submarine", anchor3, new Vector3(0f, -6f)); submarineButton.onClick.AddListener( () => { Debug.Log("Submarine"); allships[2].setBackColor(Color.blue); currentlySelectedShip = allships[2]; } ); Button cruiserButton = createWorldButton("Cruiser", anchor3, new Vector3(0f, -9f)); cruiserButton.onClick.AddListener( () => { Debug.Log("Cruiser"); currentlySelectedShip = allships[1]; } ); Button destroyerButton = createWorldButton("Destroyer", anchor3, new Vector3(0f, -12f)); destroyerButton.onClick.AddListener( () => { Debug.Log("Destroyer"); currentlySelectedShip = allships[0]; } ); //the position of the ship selection grid. anchor3.transform.position = new Vector3(10f, -4f); enemyGrid = GenerateGrid(anchor2); enemyGrid.parent.transform.position = new Vector3(10f, 10f); enemyGrid.parent.transform.localScale = new Vector3(1.5f, 1.5f); enemyGrid.makeClickableEnemy(); theTimer = Instantiate(timerText, new Vector3(-18f, 19f), Quaternion.identity); session = new gameSession(allships); StartCoroutine(BeginGame()); }