NewTetrisScore.cpp
NewTetrisScore.cpp
// newTetris.cpp : Defines the entry point for the console application.
//
#include
"stdafx.h"
#include
<
stdlib
.
h
>
#include
<
iostream
>
#include
<
time
.
h
>
#include
<
windows
.
h
>
#define
GAME_INTERVAL
20
#define
DIR_DOWN
2
#define
DIR_LEFT
4
#define
DIR_RIGHT
6
#define
DIR_ROTATE
5
using
namespace
std
;
class
TetrisShape
{
public
:
char
shapeArray
[
4
][
4
];
int
shapeTopLeftX
=
6
;
int
shapeTopLeftY
=
0
;
void
populateShapeArray
(
int
shape
);
void
rotate
();
template
<
size_t rows
,
size_t cols
>
void
setShape
(
char
(
&
shape
)[
rows
][
cols
]);
TetrisShape
(
int
shape
)
{
populateShapeArray
(
shape
);
};
TetrisShape
()
{};
};
void
TetrisShape
::
rotate
()
{
char
_shapeArray
[
4
][
4
];
_shapeArray
[
0
][
0
]
=
shapeArray
[
0
][
3
];
_shapeArray
[
1
][
0
]
=
shapeArray
[
0
][
2
];
_shapeArray
[
2
][
0
]
=
shapeArray
[
0
][
1
];
_shapeArray
[
3
][
0
]
=
shapeArray
[
0
][
0
];
_shapeArray
[
0
][
1
]
=
shapeArray
[
1
][
3
];
_shapeArray
[
1
][
1
]
=
shapeArray
[
1
][
2
];
_shapeArray
[
2
][
1
]
=
shapeArray
[
1
][
1
];
_shapeArray
[
3
][
1
]
=
shapeArray
[
1
][
0
];
_shapeArray
[
0
][
2
]
=
shapeArray
[
2
][
3
];
_shapeArray
[
1
][
2
]
=
shapeArray
[
2
][
2
];
_shapeArray
[
2
][
2
]
=
shapeArray
[
2
][
1
];
_shapeArray
[
3
][
2
]
=
shapeArray
[
2
][
0
];
_shapeArray
[
0
][
3
]
=
shapeArray
[
3
][
3
];
_shapeArray
[
1
][
3
]
=
shapeArray
[
3
][
2
];
_shapeArray
[
2
][
3
]
=
shapeArray
[
3
][
1
];
_shapeArray
[
3
][
3
]
=
shapeArray
[
3
][
0
];
for
(
int
_x
=
0
;
_x
<
4
;
_x
++
)
{
for
(
int
_y
=
0
;
_y
<
4
;
_y
++
)
{
shapeArray
[
_x
][
_y
]
=
_shapeArray
[
_x
][
_y
];
}
}
}
void
TetrisShape
::
populateShapeArray
(
int
shape
)
{
switch
(
shape
)
{
case
1
:
shapeArray
[
0
][
0
]
=
' '
;
shapeArray
[
1
][
0
]
=
' '
;
shapeArray
[
2
][
0
]
=
' '
;
shapeArray
[
3
][
0
]
=
' '
;
shapeArray
[
0
][
1
]
=
' '
;
shapeArray
[
1
][
1
]
=
'X'
;
shapeArray
[
2
][
1
]
=
' '
;
shapeArray
[
3
][
1
]
=
' '
;
shapeArray
[
0
][
2
]
=
' '
;
shapeArray
[
1
][
2
]
=
'X'
;
shapeArray
[
2
][
2
]
=
' '
;
shapeArray
[
3
][
2
]
=
' '
;
shapeArray
[
0
][
3
]
=
' '
;
shapeArray
[
1
][
3
]
=
'X'
;
shapeArray
[
2
][
3
]
=
'X'
;
shapeArray
[
3
][
3
]
=
' '
;
break
;
case
2
:
shapeArray
[
0
][
0
]
=
' '
;
shapeArray
[
1
][
0
]
=
'X'
;
shapeArray
[
2
][
0
]
=
' '
;
shapeArray
[
3
][
0
]
=
' '
;
shapeArray
[
0
][
1
]
=
' '
;
shapeArray
[
1
][
1
]
=
'X'
;
shapeArray
[
2
][
1
]
=
' '
;
shapeArray
[
3
][
1
]
=
' '
;
shapeArray
[
0
][
2
]
=
' '
;
shapeArray
[
1
][
2
]
=
'X'
;
shapeArray
[
2
][
2
]
=
' '
;
shapeArray
[
3
][
2
]
=
' '
;
shapeArray
[
0
][
3
]
=
' '
;
shapeArray
[
1
][
3
]
=
'X'
;
shapeArray
[
2
][
3
]
=
' '
;
shapeArray
[
3
][
3
]
=
' '
;
break
;
case
3
:
shapeArray
[
0
][
0
]
=
' '
;
shapeArray
[
1
][
0
]
=
' '
;
shapeArray
[
2
][
0
]
=
' '
;
shapeArray
[
3
][
0
]
=
' '
;
shapeArray
[
0
][
1
]
=
' '
;
shapeArray
[
1
][
1
]
=
'X'
;
shapeArray
[
2
][
1
]
=
' '
;
shapeArray
[
3
][
1
]
=
' '
;
shapeArray
[
0
][
2
]
=
' '
;
shapeArray
[
1
][
2
]
=
'X'
;
shapeArray
[
2
][
2
]
=
'X'
;
shapeArray
[
3
][
2
]
=
' '
;
shapeArray
[
0
][
3
]
=
' '
;
shapeArray
[
1
][
3
]
=
' '
;
shapeArray
[
2
][
3
]
=
'X'
;
shapeArray
[
3
][
3
]
=
' '
;
break
;
case
4
:
shapeArray
[
0
][
0
]
=
' '
;
shapeArray
[
1
][
0
]
=
' '
;
shapeArray
[
2
][
0
]
=
' '
;
shapeArray
[
3
][
0
]
=
' '
;
shapeArray
[
0
][
1
]
=
' '
;
shapeArray
[
1
][
1
]
=
' '
;
shapeArray
[
2
][
1
]
=
'X'
;
shapeArray
[
3
][
1
]
=
' '
;
shapeArray
[
0
][
2
]
=
' '
;
shapeArray
[
1
][
2
]
=
'X'
;
shapeArray
[
2
][
2
]
=
'X'
;
shapeArray
[
3
][
2
]
=
' '
;
shapeArray
[
0
][
3
]
=
' '
;
shapeArray
[
1
][
3
]
=
'X'
;
shapeArray
[
2
][
3
]
=
' '
;
shapeArray
[
3
][
3
]
=
' '
;
break
;
case
5
:
shapeArray
[
0
][
0
]
=
' '
;
shapeArray
[
1
][
0
]
=
' '
;
shapeArray
[
2
][
0
]
=
' '
;
shapeArray
[
3
][
0
]
=
' '
;
shapeArray
[
0
][
1
]
=
' '
;
shapeArray
[
1
][
1
]
=
' '
;
shapeArray
[
2
][
1
]
=
'X'
;
shapeArray
[
3
][
1
]
=
' '
;
shapeArray
[
0
][
2
]
=
' '
;
shapeArray
[
1
][
2
]
=
' '
;
shapeArray
[
2
][
2
]
=
'X'
;
shapeArray
[
3
][
2
]
=
' '
;
shapeArray
[
0
][
3
]
=
' '
;
shapeArray
[
1
][
3
]
=
'X'
;
shapeArray
[
2
][
3
]
=
'X'
;
shapeArray
[
3
][
3
]
=
' '
;
break
;
case
6
:
shapeArray
[
0
][
0
]
=
' '
;
shapeArray
[
1
][
0
]
=
' '
;
shapeArray
[
2
][
0
]
=
' '
;
shapeArray
[
3
][
0
]
=
' '
;
shapeArray
[
0
][
1
]
=
' '
;
shapeArray
[
1
][
1
]
=
' '
;
shapeArray
[
2
][
1
]
=
' '
;
shapeArray
[
3
][
1
]
=
' '
;
shapeArray
[
0
][
2
]
=
' '
;
shapeArray
[
1
][
2
]
=
'X'
;
shapeArray
[
2
][
2
]
=
'X'
;
shapeArray
[
3
][
2
]
=
' '
;
shapeArray
[
0
][
3
]
=
' '
;
shapeArray
[
1
][
3
]
=
'X'
;
shapeArray
[
2
][
3
]
=
'X'
;
shapeArray
[
3
][
3
]
=
' '
;
break
;
}
}
int
score
=
0
;
int
currentShape
=
-
1
;
// this is going to represent the shape that is currently in play.
bool
isDropping
=
false
;
// global that defines if a piece is currently falling - mainly for gameTick function.
int
currentTick
=
0
;
template
<
size_t rows
,
size_t cols
>
void
generateBucket
(
char
(
&
bucket
)[
rows
][
cols
]);
// Creates a slightly pre-filled bucket.
void
generateShapeStream
();
// Generate a stream of shapes.
void
dropShape
();
// Draw the shape top/center.
bool
moveShape
(
int
direction
);
// Move the shape in the spec. dir.
template
<
size_t rows
,
size_t cols
>
bool
gameTick
(
char
(
&
bucket
)[
rows
][
cols
],
char
(
&
perm_bucket
)[
rows
][
cols
]);
// Handles what is going on in the game every second.
template
<
size_t rows
,
size_t cols
>
void
landShape
(
char
(
&
bucket
)[
rows
][
cols
]);
// What to do when the shape hits the bottom.
template
<
size_t rows
,
size_t cols
>
int
checkScore
(
char
(
&
bucket
)[
rows
][
cols
],
char
(
&
perm_bucket
)[
rows
][
cols
],
int
tempvar
,
int
score
);
template
<
size_t rows
,
size_t cols
>
void
drawBucket
(
char
(
&
bucket
)[
rows
][
cols
]);
// Draws the current contents of the bucket.
template
<
size_t rows
,
size_t cols
>
bool
canEnter
(
int
direction
,
char
(
&
bucket
)[
rows
][
cols
]);
// Checks if the shape can enter the space it is trying to drop into.
int
getUserInput
();
// gets the key pressed from the user.
void
setCursorTo
(
int
x
,
int
y
);
// Move the cursor to the appropriate position
int
previousX
=
6
,
previousY
=
0
;
int
shapes
[
256
];
template
<
size_t rows
,
size_t cols
>
int
check_bucket
(
char
(
&
bucket
)[
rows
][
cols
]);
template
<
size_t rows
,
size_t cols
>
void
set_bucket
(
char
(
&
bucket
)[
rows
][
cols
],
char
(
&
perm_bucket
)[
rows
][
cols
]);
TetrisShape
activeShape
;
int
main
()
{
// Two bucket arrays, one shown on the screen, the other with the permanent contents of the buckets (walls and any non-moving shapes)
char
bucket
[
12
][
25
];
int
score
=
0
;
int
tempvar
=
0
;
char
_bucket
[
12
][
25
];
int
shapes
[
256
]
=
{};
int
shapeIndex
=
0
;
bool
gameOver
=
false
;
generateBucket
(
bucket
);
generateBucket
(
_bucket
);
generateShapeStream
();
drawBucket
(
bucket
);
while
(
!
gameOver
)
{
gameOver
=
gameTick
(
bucket
,
_bucket
);
Sleep
(
50
);
checkScore
(
bucket
,
_bucket
,
tempvar
,
score
);
cout
<<
"Your Score is: "
<<
score
<<
endl
;
currentTick
++
;
}
setCursorTo
(
25
,
6
);
cout
<<
"GAME OVER"
;
system
(
"pause"
);
}
void
setCursorTo
(
int
x
,
int
y
)
{
HANDLE handle
;
COORD position
;
handle
=
GetStdHandle
(
STD_OUTPUT_HANDLE
);
position
.
X
=
x
;
position
.
Y
=
y
;
SetConsoleCursorPosition
(
handle
,
position
);
}
/* generateBucket - takes a bucket array of any size and
* creates a semi-empty bucket, with a
* few random shapes in the bottom few lines. */
template
<
size_t rows
,
size_t cols
>
void
generateBucket
(
char
(
&
bucket
)[
rows
][
cols
])
{
for
(
int
w
=
0
;
w
<
12
;
w
++
)
{
for
(
int
z
=
0
;
z
<
25
;
z
++
)
{
if
(((
w
==
0
)
||
(
w
==
11
))
&&
(
z
==
0
))
{
bucket
[
w
][
z
]
=
'.'
;
}
else
if
(((
w
%
12
==
0
)
||
(
w
%
12
==
11
))
&&
((
z
>
0
)
&&
(
z
<
24
)))
{
bucket
[
w
][
z
]
=
'|'
;
}
else
if
(((
w
==
0
)
||
(
w
==
11
))
&&
(
z
==
24
))
{
bucket
[
w
][
z
]
=
'+'
;
}
else
if
(
z
==
24
)
{
bucket
[
w
][
z
]
=
'-'
;
}
else
{
bucket
[
w
][
z
]
=
' '
;
}
}
}
}
/* generateShapeStream - generates a pre-determined list of
* shapes that will fall from the sky. */
void
generateShapeStream
()
{
// Initialize the random number generator
srand
(
time
(
NULL
));
for
(
int
i
=
0
;
i
<
256
;
i
++
)
{
shapes
[
i
]
=
rand
()
%
6
+
1
;
}
//cout << "In generate shape..." << endl;
}
/* drawBucket - draws the actual bucket on the screen
* including the currently dropping shape. */
template
<
size_t rows
,
size_t cols
>
void
drawBucket
(
char
(
&
bucket
)[
rows
][
cols
])
{
setCursorTo
(
0
,
0
);
for
(
int
w
=
0
;
w
<
25
;
w
++
)
{
for
(
int
z
=
0
;
z
<
12
;
z
++
)
{
cout
<<
bucket
[
z
][
w
];
}
cout
<<
endl
;
}
}
/* gameTick - this function does all of the different
* processessing that happens throughout
* the game. This also returns false to
* stop the main loop once gameover has
* been reached */
template
<
size_t rows
,
size_t cols
>
bool
gameTick
(
char
(
&
bucket
)[
rows
][
cols
],
char
(
&
perm_bucket
)[
rows
][
cols
])
{
drawBucket
(
bucket
);
if
(
!
isDropping
)
{
currentShape
++
;
activeShape
=
TetrisShape
(
shapes
[
currentShape
]);
if
(
!
canEnter
(
DIR_DOWN
,
perm_bucket
))
{
return
true
;
}
else
{
isDropping
=
true
;
updateBucket
(
bucket
,
false
);
}
}
else
{
if
(
currentTick
%
GAME_INTERVAL
==
1
)
{
// we are on a drop interval.
if
(
canEnter
(
DIR_DOWN
,
perm_bucket
))
{
updateBucket
(
bucket
,
moveShape
(
DIR_DOWN
));
}
else
{
landShape
(
perm_bucket
);
}
}
}
int
direction
=
getUserInput
();
if
(
canEnter
(
direction
,
perm_bucket
))
{
updateBucket
(
bucket
,
moveShape
(
direction
));
}
if
(
!
canEnter
(
DIR_DOWN
,
perm_bucket
))
{
landShape
(
perm_bucket
);
set_bucket
(
bucket
,
perm_bucket
);
}
return
false
;
}
/* moveShape - Handles moving the shape in the bucket. */
bool
moveShape
(
int
direction
)
{
previousX
=
activeShape
.
shapeTopLeftX
;
previousY
=
activeShape
.
shapeTopLeftY
;
switch
(
direction
)
{
case
DIR_DOWN
:
activeShape
.
shapeTopLeftY
++
;
return
false
;
break
;
case
DIR_RIGHT
:
activeShape
.
shapeTopLeftX
++
;
return
false
;
break
;
case
DIR_LEFT
:
activeShape
.
shapeTopLeftX
--
;
return
false
;
break
;
case
DIR_ROTATE
:
activeShape
.
rotate
();
return
true
;
break
;