Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

600610795 lab 5 #6

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void AddModel(Model m)
mList.Add(m);
}

// virtual keyword allow the method to be overriden
// The `virtual` keyword allows the method to be overridden
public virtual void ActionPerformed(int action)
{
throw new NotImplementedException();
Expand Down
69 changes: 57 additions & 12 deletions TwoZeroFourEightModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace twozerofoureight
{
Expand All @@ -11,15 +12,15 @@ class TwoZeroFourEightModel : Model
protected int boardSize; // default is 4
protected int[,] board;
protected Random rand;
protected int score = 0;

public TwoZeroFourEightModel() : this(4)
{
// default board size is 4
// default board size is 4
}

public int[,] GetBoard()
public string UpdateScore()
{
return board;
return score.ToString();
}

public TwoZeroFourEightModel(int size)
Expand All @@ -37,9 +38,27 @@ public TwoZeroFourEightModel(int size)
NotifyAll();
}

public bool isfull()
{
for(int i = 0; i < boardSize; i++)
{
for(int j=0; j < boardSize; j++)
{
if (board[i, j] == 0) return false;
}
}
GameOver();
return true;
}

public int[,] GetBoard()
{
return board;
}

private int[,] Random(int[,] input)
{
while (true)
while (!isfull())
{
int x = rand.Next(boardSize);
int y = rand.Next(boardSize);
Expand All @@ -50,6 +69,27 @@ public TwoZeroFourEightModel(int size)
}
}
return input;

}

public void GameOver()
{
bool a=true;
for (int i = 0; i < boardSize-1; i++)
{
for (int j = 0; j < boardSize-1; j++)
{
if (board[i, j] == board[i, j + 1]|| board[j, i] == board[j + 1, i])
a = false;
}
}
for(int i = 0; i < 3; i++)
{
if (board[i, 3] == board[i + 1, 3]||board[3,i]==board[3,i+1])
a = false;
}
if (a)
MessageBox.Show("GAME OVER");
}

public void PerformDown()
Expand All @@ -67,12 +107,13 @@ public void PerformDown()
{
buffer[k] = 0;
}
//shift left
// shift down
foreach (int j in rangeY)
{
if (board[j, i] != 0)
{
buffer[pos] = board[j, i];

pos++;
}
}
Expand All @@ -82,10 +123,11 @@ public void PerformDown()
if (j > 0 && buffer[j] != 0 && buffer[j] == buffer[j - 1])
{
buffer[j - 1] *= 2;
score += buffer[j - 1];
buffer[j] = 0;
}
}
// shift left again
// shift down again
pos = 3;
foreach (int j in rangeX)
{
Expand Down Expand Up @@ -119,7 +161,7 @@ public void PerformUp()
{
buffer[k] = 0;
}
//shift left
// shift up
foreach (int j in range)
{
if (board[j, i] != 0)
Expand All @@ -134,10 +176,11 @@ public void PerformUp()
if (j > 0 && buffer[j] != 0 && buffer[j] == buffer[j - 1])
{
buffer[j - 1] *= 2;
score += buffer[j - 1];
buffer[j] = 0;
}
}
// shift left again
// shift up again
pos = 0;
foreach (int j in range)
{
Expand Down Expand Up @@ -173,7 +216,7 @@ public void PerformRight()
{
buffer[k] = 0;
}
//shift left
// shift right
foreach (int j in rangeX)
{
if (board[i, j] != 0)
Expand All @@ -188,10 +231,11 @@ public void PerformRight()
if (j > 0 && buffer[j] != 0 && buffer[j] == buffer[j - 1])
{
buffer[j - 1] *= 2;
score += buffer[j - 1];
buffer[j] = 0;
}
}
// shift left again
// shift right again
pos = 3;
foreach (int j in rangeY)
{
Expand Down Expand Up @@ -224,7 +268,7 @@ public void PerformLeft()
{
buffer[k] = 0;
}
//shift left
// shift left
foreach (int j in range)
{
if (board[i, j] != 0)
Expand All @@ -239,6 +283,7 @@ public void PerformLeft()
if (j > 0 && buffer[j] != 0 && buffer[j] == buffer[j - 1])
{
buffer[j - 1] *= 2;
score += buffer[j - 1];
buffer[j] = 0;
}
}
Expand Down
Loading