-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.cs
122 lines (106 loc) · 2.72 KB
/
Cell.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tetris
{
class Cell : IComparable
{
private readonly bool moving;
public bool Moving { get { return moving; } }
readonly int x;
public int X
{
get
{
if (moving)
return x;
throw new NotMovingPointException();
}
}
readonly int y;
public int Y
{
get
{
if (moving)
return y;
throw new NotMovingPointException();
}
}
public Cell(int x, int y,bool moving = true)
{
this.moving = moving;
this.x = x;
this.y = y;
}
public int GetNotMovingX()
{
return x;
}
public int GetNotMovingY()
{
return y;
}
public Cell MoveTowards(int deltaX)
{
return new Cell(x + deltaX, y,true);
}
public Cell MoveDown(int deltaY)
{
return new Cell(x, y + deltaY,true);
}
public override string ToString()
{
return moving ? "*" : "#";
}
public Cell StartMoving()
{
return new Cell(x,y,true);
}
public Cell StopMoving()
{
return new Cell(x,y,false);
}
public override bool Equals(object obj)
{
if (obj is Cell)
{
var otherCell = obj as Cell;
return x == otherCell.x;// && y == otherCell.y;
}
return false;
}
public override int GetHashCode()
{
return Y*10000 + x;
}
public static bool operator >(Cell firstCell, Cell otheCell)
{
return firstCell.x > otheCell.x;
}
public static bool operator <(Cell firstCell, Cell otheCell)
{
return firstCell.x < otheCell.x;
}
public int CompareTo(object obj)
{
if (!(obj is Cell))
throw new ArgumentException();
var cell = obj as Cell;
if (this > cell)
return 1;
if (this.Equals(cell))
return 0;
return -1;
}
}
class NotMovingPointException : Exception
{
public override string Message
{
get { return "This cell's x and y can be wrong"; }
}
}
}