Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Game2048
- {
- public class Model
- {
- private int score = 0;
- private Map map;
- private Random random = new Random();
- private bool isGameOver;
- private bool moved;
- public int Score => this.score;
- public int size => this.map.size;
- public Model(int size) => this.map = new Map(size);
- public void Start()
- {
- this.score = 0;
- this.isGameOver = false;
- for (int x = 0; x < this.size; ++x)
- {
- for (int y = 0; y < this.size; ++y)
- this.map.Set(x, y, 0);
- }
- this.AddRandomNumber();
- this.AddRandomNumber();
- }
- private void AddRandomNumber()
- {
- if (this.isGameOver)
- return;
- List<int[]> source = new List<int[]>();
- for (int x = 0; x < this.size; ++x)
- {
- for (int y = 0; y < this.size; ++y)
- {
- if (this.GetMap(x, y) == 0)
- source.Add(new int[2]{ x, y });
- }
- }
- int index = this.random.Next(0, source.Count<int[]>());
- int number = this.random.Next(1, 3) * 2;
- this.map.Set(source[index][0], source[index][1], number);
- }
- private void Join(int x, int y, int sx, int sy)
- {
- if (this.map.Get(x, y) <= 0 || this.map.Get(x + sx, y + sy) != this.map.Get(x, y))
- return;
- this.map.Set(x + sx, y + sy, this.map.Get(x, y) * 2);
- this.score += this.map.Get(x, y) * this.map.Get(x, y);
- for (; this.map.Get(x - sx, y - sy) > 0; y -= sy)
- {
- this.map.Set(x, y, this.map.Get(x - sx, y - sy));
- x -= sx;
- }
- this.map.Set(x, y, 0);
- this.moved = true;
- }
- private void Lift(int x, int y, int sx, int sy)
- {
- if (this.map.Get(x, y) <= 0)
- return;
- while (this.map.Get(x + sx, y + sy) == 0)
- {
- this.map.Set(x + sx, y + sy, this.map.Get(x, y));
- this.map.Set(x, y, 0);
- x += sx;
- y += sy;
- this.moved = true;
- }
- }
- public void Left()
- {
- this.moved = false;
- for (int y = 0; y < this.map.size; ++y)
- {
- for (int x = 1; x < this.map.size; ++x)
- this.Lift(x, y, -1, 0);
- }
- for (int y = 0; y < this.map.size; ++y)
- {
- for (int x = 1; x < this.map.size; ++x)
- this.Join(x, y, -1, 0);
- }
- if (!this.moved)
- return;
- this.AddRandomNumber();
- }
- public void Right()
- {
- this.moved = false;
- for (int y = 0; y < this.map.size; ++y)
- {
- for (int x = this.map.size - 2; x >= 0; --x)
- this.Lift(x, y, 1, 0);
- for (int x = this.map.size - 2; x >= 0; --x)
- this.Join(x, y, 1, 0);
- }
- if (!this.moved)
- return;
- this.AddRandomNumber();
- }
- public void Up()
- {
- this.moved = false;
- for (int x = 0; x < this.map.size; ++x)
- {
- for (int y = 1; y < this.map.size; ++y)
- this.Lift(x, y, 0, -1);
- }
- for (int x = 0; x < this.map.size; ++x)
- {
- for (int y = 1; y < this.map.size; ++y)
- this.Join(x, y, 0, -1);
- }
- if (!this.moved)
- return;
- this.AddRandomNumber();
- }
- public void Down()
- {
- this.moved = false;
- for (int x = 0; x < this.map.size; ++x)
- {
- for (int y = this.map.size - 2; y >= 0; --y)
- this.Lift(x, y, 0, 1);
- }
- for (int x = 0; x < this.map.size; ++x)
- {
- for (int y = this.map.size - 2; y >= 0; --y)
- this.Join(x, y, 0, 1);
- }
- if (!this.moved)
- return;
- this.AddRandomNumber();
- }
- public int GetMap(int x, int y) => this.map.Get(x, y);
- public bool IsGameOver()
- {
- if (this.isGameOver)
- return this.isGameOver;
- for (int x = 0; x < this.map.size; ++x)
- {
- for (int y = 0; y < this.map.size; ++y)
- {
- if (this.map.Get(x, y) == 0)
- return false;
- }
- }
- for (int x = 0; x < this.map.size; ++x)
- {
- for (int y = 0; y < this.map.size; ++y)
- {
- if (this.map.Get(x, y) == this.map.Get(x + 1, y) || this.map.Get(x, y) == this.map.Get(x, y + 1))
- return false;
- }
- }
- this.isGameOver = true;
- return this.isGameOver;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement