//mengurangi nilai matrix ketika pentomino dikeluarkan dari board public void delMatrixBoard(int i, int j, Pentominos p) { int x, y; Boolean oke = false; for (int a = 0; a < 5; a++) { for (int b = 0; b < 5; b++) { if (p.getMatrix()[a, b] == 1) { oke = true; x = i + a; y = j + b; matrix[x, y]--; } else { if (!oke) { j--; } } } } }
//melakukan penempatan pentomino pada posisi x, y public void place_pentomino(Pentominos p, int x, int y) { int lalaye = 0; Boolean laye = false; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (p.getMatrix()[i, j] == 1) { p.SetPos(x, y - lalaye); laye = true; break; } else { lalaye += 25; } } if (laye) { break; } } }
//mengecek apakah terdapat pentomino yang bertumpukan public Boolean pentominoCollision(Pentominos p, int z) { for (int j = 0; j < 12; j++) { for (int a = 0; a < 5; a++) { for (int b = 0; b < 5; b++) { if (p.getMatrix()[a, b] == 1) { for (int c = 0; c < 5; c++) { for (int d = 0; d < 5; d++) { if (pentomino[j].getMatrix()[c, d] == 1) { if (p.GetX() + (a * 25) == pentomino[j].GetX() + (c * 25) && p.GetY() + (b * 25) == pentomino[j].GetY() + (d * 25) && z != j) { return(true); } } } } } } } } return(false); }
//menghandle penekanan tombol pada mouse private void frmPentominos_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if (player) { if (e.Button == MouseButtons.Right && draggedpentomino != null) { sp2.Play(); draggedpentomino.klikkanan(); Invalidate(new Rectangle(draggedpentomino.GetX() - 2, draggedpentomino.GetY() - 2, 150, 150)); } if (e.Button == MouseButtons.Left && draggedpentomino != null) { if (!pentominoCollision(draggedpentomino, draggedpentomino.getUrutan())) { new System.Media.SoundPlayer(Properties.Resources.shoot).Play(); draggedpentomino = null; isFinish(); } return; } if (e.Button == MouseButtons.Left && draggedpentomino == null) { int iPosX = e.X / 25; int iPosY = e.Y / 25; iPosX *= 25; iPosY *= 25; for (int i = 0; i < 12; i++) { if (pentomino[i].atPos(iPosX, iPosY)) { draggedpentomino = pentomino[i]; new System.Media.SoundPlayer(Properties.Resources.shoot).Play(); break; } } } } else if (editorB) { if (e.Button == MouseButtons.Left) { int iPosX = e.X / 25; int iPosY = e.Y / 25; iPosX *= 25; iPosY *= 25; set = editor.getSel(iPosX, iPosY); editor.atPos(iPosX, iPosY, set); Invalidate(); } } }
//mengembalikan pentomino kembali ke awal berdasarkan pentomino awal public void setBackPentaminos(Pentominos p) { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { matrix[i, j] = p.matrix[i, j]; } } turn = p.turn; placed = false; }
// mengeset nilai ketika ada pentomino dimasukan public Boolean setMatrixBoard(int i, int j, Pentominos p) { int x, y; int ia = i; int ja = j; Boolean notvalid = false; Boolean oke = false; for (int a = 0; a < 5; a++) { for (int b = 0; b < 5; b++) { if (p.getMatrix()[a, b] == 1) { oke = true; x = i + a; y = j + b; if (!isValid(i, j, p)) { notvalid = true; break; } matrix[x, y]++; } else { if (!oke) { j--; } } } if (notvalid) { break; } } if (notvalid) { return(false); } if (isCollisionOnBoard()) { delMatrixBoard(ia, ja, p); return(false); } else { return(true); } }
//untuk melakukan random pada inisial pentomino public void randomPentominos() { Random rnd = new Random(); Queue <int> N = new Queue <int>(); List <int> Nu = new List <int>(); int lol; int[] la = new int[12]; int pop; for (int i = 0; i < 12; i++) { Nu.Add(i); } for (int i = 0; i < 12; i++) { lol = rnd.Next(0, Nu.Count()); pop = Nu.ElementAt(lol) + 1; Nu.RemoveAt(lol); pentomino[i] = new Pentominos(); pentomino[i].Createpentominos(pop, i); initialpentomino[i] = new Pentominos(); initialpentomino[i].Createpentominos(pop, i); lol = rnd.Next(0, pentomino[i].getJRotate()); while (lol > 0) { pentomino[i].klikkanan(); initialpentomino[i].klikkanan(); lol--; } } }
//apakah valid, pentomino yang ditempatkan diboard berada di wilayah board public Boolean isValid(int i, int j, Pentominos p) { return(i >= 0 && j >= 0 && i + p.GetWidthM() <= Cols && j + p.GetHeightM() <= Rows); }