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

600610792 Lab5 #91

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
1 change: 1 addition & 0 deletions Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace twozerofoureight
class Controller
{
protected ArrayList mList;


public Controller()
{
Expand Down
20 changes: 17 additions & 3 deletions TwoZeroFourEightController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
using System.Text;
using System.Threading.Tasks;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace twozerofoureight
{
class TwoZeroFourEightController : Controller
Expand All @@ -12,21 +22,22 @@ class TwoZeroFourEightController : Controller
public const int RIGHT = 1;
public const int UP = 2;
public const int DOWN = 3;

public TwoZeroFourEightController()
{

}



public override void ActionPerformed(int action)
{
foreach (TwoZeroFourEightModel m in mList)
{
switch (action)
{
case LEFT:
case LEFT :
m.PerformLeft();
break;
break;
case RIGHT:
m.PerformRight();
break;
Expand All @@ -36,9 +47,12 @@ public override void ActionPerformed(int action)
case DOWN:
m.PerformDown();
break;


}

}
}

}
}
114 changes: 108 additions & 6 deletions TwoZeroFourEightModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,26 @@ class TwoZeroFourEightModel : Model
protected int boardSize; // default is 4
protected int[,] board;
protected Random rand;

public int boardsize = 0;
public bool isfull = true;
public int score = 0;
public TwoZeroFourEightModel() : this(4)
{
// default board size is 4
}

public int GetScore()
{
score = 0;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
score += board[i, j];
}
}
return score;
}
public int[,] GetBoard()
{
return board;
Expand All @@ -27,28 +41,82 @@ public TwoZeroFourEightModel(int size)
boardSize = size;
board = new int[boardSize, boardSize];
var range = Enumerable.Range(0, boardSize);
foreach(int i in range) {
foreach(int j in range) {
board[i,j] = 0;
foreach (int i in range)
{
foreach (int j in range)
{
board[i, j] = 0;
}
}
rand = new Random();
board = Random(board);
NotifyAll();
}

private void checkfull()
{
boardsize = 0;
isfull = false;

for (int i =0;i<4;i++)
{
for (int j =0;j<4;j++)
{
if (board[j, i] != 0)
{

boardsize++;
if (boardsize >= 16)
{
boardsize = 16;

break;
}

}
// score += board[i, j];

}
}



}

private int[,] Random(int[,] input)
{

while (true)
{
int x = rand.Next(boardSize);
int y = rand.Next(boardSize);
int temp = 0;

if (board[x, y] == 0)
{
board[x, y] = 2;
break;
}
else
{

while (board[x, y] != 0)
{
temp++;
x = rand.Next(boardSize);
y = rand.Next(boardSize);
if (board[x, y] == 0) // find new position
{
board[x, y] = 2;
break;
}
else if (temp >= 16) break; // force to break

}
break;
}
}
checkfull();
return input;
}

Expand Down Expand Up @@ -109,7 +177,6 @@ public void PerformUp()
{
int[] buffer;
int pos;

int[] range = Enumerable.Range(0, boardSize).ToArray();
foreach (int i in range)
{
Expand All @@ -135,7 +202,9 @@ public void PerformUp()
{
buffer[j - 1] *= 2;
buffer[j] = 0;

}

}
// shift left again
pos = 0;
Expand All @@ -161,7 +230,6 @@ public void PerformRight()
{
int[] buffer;
int pos;

int[] rangeX = Enumerable.Range(0, boardSize).ToArray();
int[] rangeY = Enumerable.Range(0, boardSize).ToArray();
Array.Reverse(rangeX);
Expand Down Expand Up @@ -189,7 +257,9 @@ public void PerformRight()
{
buffer[j - 1] *= 2;
buffer[j] = 0;

}

}
// shift left again
pos = 3;
Expand Down Expand Up @@ -260,5 +330,37 @@ public void PerformLeft()
board = Random(board);
NotifyAll();
}

public bool Check()
{
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]|| board[i,j]==0)
a = false;
}
}
for (int i = 0; i < boardSize; i++)
{
for (int j = 0; j < boardSize; j++)
{
if (board[i, j] == 0)
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;
}

return a;
}




}
}
Loading