Add some tests converted from TD

This commit is contained in:
Michal Moskal 2016-07-22 15:18:38 +01:00
parent 3e37b03808
commit a5d5836ea2
4 changed files with 619 additions and 1 deletions

3
.gitignore vendored
View File

@ -1,6 +1,7 @@
node_modules node_modules
yotta_modules yotta_modules
yotta_targets yotta_targets
pxt_modules
built built
typings typings
tmp tmp
@ -18,4 +19,4 @@ clients/**/obj/**
*.tgz *.tgz
*.db *.db
*.suo *.suo
*.log *.log

232
tests/hat-game.ts Normal file
View File

@ -0,0 +1,232 @@
let correctBall: number
let ballRevealing: boolean
let cupSelect: string
let index: number
let score: number
let level: number
let swapSpeed: number
initializeGame()
input.onButtonPressed(Button.A, () => {
if (ballRevealing) {
index = index + 1
if (index > 2) {
index = 0
}
basic.showString(cupSelect[index], 150)
}
})
input.onButtonPressed(Button.B, () => {
if (ballRevealing) {
ballRevealing = false
if (correctBall == index) {
score = score + level
images.createImage(`
. . . . .
. . . . #
. . . # .
# . # . .
. # . . .
`).showImage(0)
basic.pause(1000)
basic.showString("+".concat(level.toString()), 150)
basic.pause(1000)
} else {
images.createImage(`
# . . . #
. # . # .
. . # . .
. # . # .
# . . . #
`).showImage(0)
basic.pause(1000)
basic.clearScreen()
revealBall(correctBall)
basic.pause(1000)
}
}
level = level + 1
if (level == 4) {
basic.showString("FINAL SCORE:", 75)
basic.showNumber(score, 150)
} else {
playLevel(level)
}
})
playLevel(1)
function revealBall(p: number) {
let xCoordinate = 2 * p
for (let j = 0; j < 3; j++) {
led.plot(j * 2, 2)
}
for (let i = 0; i < 3; i++) {
led.unplot(xCoordinate, 2)
led.plot(xCoordinate, 1)
basic.pause(100)
led.unplot(xCoordinate, 1)
led.plot(xCoordinate, 0)
basic.pause(200)
led.unplot(xCoordinate, 0)
led.plot(xCoordinate, 1)
basic.pause(100)
led.unplot(xCoordinate, 1)
led.plot(xCoordinate, 2)
basic.pause(75)
}
basic.pause(1000)
}
function initializeGame() {
ballRevealing = false
level = 1
score = 0
cupSelect = "LMR"
}
function swapCups(cup_1: number, cup_2: number, pauseDifficulty: number) {
let cup_1X = 2 * cup_1
let cup_2X = 2 * cup_2
let cupXAverage = (cup_1X + cup_2X) / 2
led.unplot(cup_1X, 2)
led.unplot(cup_2X, 2)
led.plot(cup_1X, 3)
led.plot(cup_2X, 1)
basic.pause(pauseDifficulty)
led.unplot(cup_1X, 3)
led.unplot(cup_2X, 1)
led.plot(cup_1X, 4)
led.plot(cup_2X, 0)
basic.pause(pauseDifficulty)
led.unplot(cup_1X, 4)
led.unplot(cup_2X, 0)
if (cupXAverage == 2) {
led.plot((cupXAverage + cup_1X) / 2, 4)
led.plot((cupXAverage + cup_2X) / 2, 0)
basic.pause(pauseDifficulty)
led.unplot((cupXAverage + cup_1X) / 2, 4)
led.unplot((cupXAverage + cup_2X) / 2, 0)
}
led.plot(cupXAverage, 4)
led.plot(cupXAverage, 0)
basic.pause(pauseDifficulty)
led.unplot(cupXAverage, 4)
led.unplot(cupXAverage, 0)
if (cupXAverage == 2) {
led.plot((cupXAverage + cup_2X) / 2, 4)
led.plot((cupXAverage + cup_1X) / 2, 0)
basic.pause(pauseDifficulty)
led.unplot((cupXAverage + cup_2X) / 2, 4)
led.unplot((cupXAverage + cup_1X) / 2, 0)
}
led.plot(cup_2X, 4)
led.plot(cup_1X, 0)
basic.pause(pauseDifficulty)
led.unplot(cup_2X, 4)
led.unplot(cup_1X, 0)
led.plot(cup_2X, 3)
led.plot(cup_1X, 1)
basic.pause(pauseDifficulty)
led.unplot(cup_2X, 3)
led.unplot(cup_1X, 1)
led.plot(cup_2X, 2)
led.plot(cup_1X, 2)
basic.pause(pauseDifficulty)
if (correctBall == cup_1) {
correctBall = cup_2
} else if (correctBall == cup_2) {
correctBall = cup_1
}
}
function swapFake(cup_1: number, cup_2: number, pauseDifficulty: number) {
let cup_1X = 2 * cup_1
let cup_2X = 2 * cup_2
let cupXAverage = (cup_1X + cup_2X) / 2
led.unplot(cup_1X, 2)
led.unplot(cup_2X, 2)
led.plot(cup_1X, 3)
led.plot(cup_2X, 1)
basic.pause(pauseDifficulty)
led.unplot(cup_1X, 3)
led.unplot(cup_2X, 1)
led.plot(cup_1X, 4)
led.plot(cup_2X, 0)
basic.pause(pauseDifficulty)
led.unplot(cup_1X, 4)
led.unplot(cup_2X, 0)
if (cupXAverage == 2) {
led.plot((cupXAverage + cup_1X) / 2, 4)
led.plot((cupXAverage + cup_2X) / 2, 0)
basic.pause(pauseDifficulty)
led.unplot((cupXAverage + cup_1X) / 2, 4)
led.unplot((cupXAverage + cup_2X) / 2, 0)
}
led.plot(cupXAverage, 4)
led.plot(cupXAverage, 0)
basic.pause(pauseDifficulty)
led.unplot(cupXAverage, 4)
led.unplot(cupXAverage, 0)
if (cupXAverage == 2) {
led.plot((cupXAverage + cup_1X) / 2, 4)
led.plot((cupXAverage + cup_2X) / 2, 0)
basic.pause(pauseDifficulty)
led.unplot((cupXAverage + cup_1X) / 2, 4)
led.unplot((cupXAverage + cup_2X) / 2, 0)
}
led.plot(cup_1X, 4)
led.plot(cup_2X, 0)
basic.pause(pauseDifficulty)
led.unplot(cup_1X, 4)
led.unplot(cup_2X, 0)
led.plot(cup_1X, 3)
led.plot(cup_2X, 1)
basic.pause(pauseDifficulty)
led.unplot(cup_1X, 3)
led.unplot(cup_2X, 1)
led.plot(cup_1X, 2)
led.plot(cup_2X, 2)
basic.pause(pauseDifficulty)
}
function playLevel(level1: number) {
basic.showNumber(level, 150)
basic.pause(3000)
basic.clearScreen()
for (let i = 0; i < 3; i++) {
led.plot(2 * i, 2)
}
basic.pause(1000)
correctBall = Math.random(3)
revealBall(correctBall)
basic.pause(1000)
let swaps = 5 + 10 * level1
if (level1 == 1) {
swapSpeed = 80
} else if (level1 == 2) {
swapSpeed = 40
} else {
swapSpeed = 20
}
for (let i1 = 0; i1 < swaps; i1++) {
let swapType = Math.random(3)
let not = Math.random(3)
if (swapType < 2) {
let swapOrientation = Math.random(2)
if (swapOrientation == 0) {
swapCups((not + 1) % 3, (not + 2) % 3, swapSpeed)
} else {
swapCups((not + 2) % 3, (not + 1) % 3, swapSpeed)
}
} else {
let swapOrientation1 = Math.random(2)
if (swapOrientation1 == 0) {
swapFake((not + 1) % 3, (not + 2) % 3, swapSpeed)
} else {
swapFake((not + 2) % 3, (not + 1) % 3, swapSpeed)
}
}
}
index = -1
ballRevealing = true
}

155
tests/meteorite.ts Normal file
View File

@ -0,0 +1,155 @@
let oneX: number
let oneY: number
let twoX: number
let twoY: number
let pause: number
let meteoriteOneX: number
let meteoriteOneY: number
let meteoriteTwoX: number
let meteoriteTwoY: number
let counter: number
basic.pause(2000)
oneX = 0
oneY = 4
twoX = 1
twoY = 4
counter = 0
pause = 700
led.plot(oneX, oneY)
led.plot(twoX, twoY)
input.onButtonPressed(Button.A, () => {
if (oneX > 0) {
led.unplot(oneX, oneY)
led.unplot(twoX, twoY)
oneX = oneX - 1
twoX = twoX - 1
led.plot(oneX, oneY)
led.plot(twoX, twoY)
}
})
input.onButtonPressed(Button.B, () => {
if (twoX < 4) {
led.unplot(oneX, oneY)
led.unplot(twoX, twoY)
oneX = oneX + 1
twoX = twoX + 1
led.plot(oneX, oneY)
led.plot(twoX, twoY)
}
})
meteoriteOneX = Math.random(5)
meteoriteOneY = 0
meteoriteTwoX = Math.random(5)
meteoriteTwoY = -3
basic.pause(1000)
for (let i = 0; i < 3; i++) {
led.plot(meteoriteTwoX, meteoriteTwoY)
led.plot(meteoriteOneX, meteoriteOneY)
basic.pause(pause)
led.unplot(meteoriteTwoX, meteoriteTwoY)
led.unplot(meteoriteOneX, meteoriteOneY)
meteoriteOneY = meteoriteOneY + 1
meteoriteTwoY = meteoriteTwoY + 1
}
basic.forever(() => {
for (let i1 = 0; i1 < 3; i1++) {
led.plot(meteoriteTwoX, meteoriteTwoY)
led.plot(meteoriteOneX, meteoriteOneY)
basic.pause(pause)
led.unplot(meteoriteOneX, meteoriteOneY)
led.unplot(meteoriteTwoX, meteoriteTwoY)
meteoriteOneY = meteoriteOneY + 1
meteoriteTwoY = meteoriteTwoY + 1
if (meteoriteOneY == 4) {
if (meteoriteOneX == oneX) {
for (let j = 0; j < 10; j++) {
led.plotAll()
basic.pause(200)
basic.clearScreen()
basic.pause(200)
}
basic.showNumber(counter, 150)
basic.pause(10000)
} else if (meteoriteOneX == twoX) {
for (let j1 = 0; j1 < 10; j1++) {
led.plotAll()
basic.pause(200)
basic.clearScreen()
basic.pause(200)
}
basic.showNumber(counter, 150)
basic.pause(10000)
}
}
}
while (Math.abs(meteoriteTwoX - meteoriteOneX) < 1) {
meteoriteOneX = Math.random(5)
}
meteoriteOneY = 0
counter = counter + 1
if (counter == 3) {
pause = pause - 250
} else if (counter == 8) {
pause = pause - 100
} else if (counter == 12) {
pause = pause - 100
} else if (counter == 20) {
pause = pause - 100
} else if (counter == 30) {
pause = pause - 70
}
if (counter == 40) {
pause = pause - 70
}
for (let i2 = 0; i2 < 3; i2++) {
led.plot(meteoriteOneX, meteoriteOneY)
led.plot(meteoriteTwoX, meteoriteTwoY)
basic.pause(pause)
led.unplot(meteoriteOneX, meteoriteOneY)
led.unplot(meteoriteTwoX, meteoriteTwoY)
meteoriteOneY = meteoriteOneY + 1
meteoriteTwoY = meteoriteTwoY + 1
if (meteoriteTwoY == 4) {
if (meteoriteTwoX == oneX) {
for (let j2 = 0; j2 < 10; j2++) {
led.plotAll()
basic.pause(200)
basic.clearScreen()
basic.pause(200)
}
basic.showNumber(counter, 150)
basic.pause(10000)
} else if (meteoriteTwoX == twoX) {
for (let j3 = 0; j3 < 10; j3++) {
led.plotAll()
basic.pause(200)
basic.clearScreen()
basic.pause(200)
}
basic.showNumber(counter, 150)
basic.pause(10000)
}
}
}
meteoriteTwoX = Math.random(5)
while (Math.abs(meteoriteTwoX - meteoriteOneX) < 1) {
meteoriteTwoX = Math.random(5)
}
meteoriteTwoY = 0
counter = counter + 1
if (counter == 3) {
pause = pause - 250
} else if (counter == 8) {
pause = pause - 100
} else if (counter == 12) {
pause = pause - 100
} else if (counter == 20) {
pause = pause - 100
} else if (counter == 30) {
pause = pause - 70
} else if (counter == 40) {
pause = pause - 70
}
})

230
tests/pac-man-runaway.ts Normal file
View File

@ -0,0 +1,230 @@
let levelTime: number
let person: Entity
let monsters: Entity[]
let totalMonsters: number
let playing: boolean
let gameSuspended: boolean
let busyPos: Point[]
class Entity {
public x: number
public y: number
public dirX: number
public dirY: number
public hitHorizontalWall(): boolean {
return this.y == 0 && this.dirY == -1 || this.y == 4 && this.dirY == 1
}
public hitVerticalWall(): boolean {
return this.x == 0 && this.dirX == -1 || this.x == 4 && this.dirX == 1
}
public possHorizontalDir(): number {
if (this.x == 0) {
return 1
} else if (this.x == 4) {
return - 1
} else {
return Math.random(2) * 2 - 1
}
}
public possVerticalDir(): number {
if (this.y == 0) {
return 1
} else if (this.y == 4) {
return - 1
} else {
return Math.random(2) * 2 - 1
}
}
public collidesX(p2: Entity): boolean {
return this.y == p2.y && this.y + this.dirY == p2.y + p2.dirY && (this.x + this.dirX == p2.x || this.x + this.dirX == p2.x + p2.dirX || p2.x + p2.dirX == this.x)
}
public collidesY(p2: Entity): boolean {
return this.x == p2.x && this.x + this.dirX == p2.x + p2.dirX && (this.y + this.dirY == p2.y || this.y + this.dirY == p2.y + p2.dirY || p2.y + p2.dirY == this.y)
}
public move1() {
this.x = this.x + this.dirX
this.y = this.y + this.dirY
}
public towardsX(p2: Entity): number {
return Math.sign(p2.x - this.x)
}
public towardsY(p2: Entity): number {
return Math.sign(p2.y - this.y)
}
public plot() {
led.plot(this.x, this.y)
}
public blink() {
led.plot(this.x, this.y)
basic.pause(125)
led.unplot(this.x, this.y)
basic.pause(125)
led.plot(this.x, this.y)
}
}
class Point {
public x: number
public y: number
}
initializeState()
redraw()
basic.pause(1000)
basic.forever(() => {
levelTime = levelTime + 12
basic.pause(12)
if (!playing) {
levelTime = 0
playing = true
}
if (levelTime >= 5000) {
gameSuspended = true
game.levelUp()
levelUp()
levelTime = 0
resetState()
redraw()
basic.pause(1000)
gameSuspended = false
}
})
basic.forever(() => {
if (!gameSuspended) {
logic()
redraw()
basic.pause(500)
}
})
input.onButtonPressed(Button.A, () => {
let temp = Math.abs(person.dirX) * (-1)
person.dirX = Math.abs(person.dirY) * (-1)
person.dirY = temp
})
input.onButtonPressed(Button.B, () => {
let temp1 = Math.abs(person.dirX)
person.dirX = Math.abs(person.dirY)
person.dirY = temp1
})
function redraw() {
basic.clearScreen()
person.plot()
for (let i = 0; i < totalMonsters; i++) {
monsters[i].blink()
}
}
function initializeState() {
person = new Entity()
playing = false
busyPos = ([] as Point[])
let busyPos1 = new Point()
busyPos1.x = 1
busyPos1.y = 1
let busyPos2 = new Point()
busyPos2.x = 1
busyPos2.y = 3
let busyPos3 = new Point()
busyPos3.x = 3
busyPos3.y = 1
busyPos.push(busyPos1)
busyPos.push(busyPos2)
busyPos.push(busyPos3)
monsters = ([] as Entity[])
addMonster()
resetState()
}
function logic() {
if (person.hitHorizontalWall()) {
person.dirY = 0
person.dirX = person.possHorizontalDir()
}
if (person.hitVerticalWall()) {
person.dirX = 0
person.dirY = person.possVerticalDir()
}
let lost = false
for (let i = 0; i < totalMonsters; i++) {
let m = monsters[i]
m.dirX = m.towardsX(person)
m.dirY = m.towardsY(person)
if (m.dirX != 0 && m.dirY != 0) {
let x = Math.random(2)
if (x == 1) {
m.dirX = 0
} else {
m.dirY = 0
}
}
if (person.collidesX(m) || person.collidesY(m)) {
lost = true
}
}
if (!lost) {
moveAll()
} else {
loseLife()
}
}
function loseLife() {
moveAll()
basic.pause(500)
basic.showLeds(`
. # . # .
. . # . .
. . . . .
. # # # .
# . . . #
`, 400)
basic.pause(1000)
basic.clearScreen()
game.removeLife(1)
playing = false
resetState()
}
function moveAll() {
person.move1()
for (let i = 0; i < totalMonsters; i++) {
monsters[i].move1()
}
}
function addMonster() {
let m = new Entity()
monsters.push(m)
totalMonsters = totalMonsters + 1
}
function levelUp() {
addMonster()
}
function resetState() {
levelTime = 0
game.setLife(5)
person.x = 4
person.y = 4
person.dirX = -1
person.dirY = 0
for (let i = 0; i < totalMonsters; i++) {
let busy = busyPos[i]
let m = monsters[i]
m.x = (busy.x + Math.random(3)) - 1
m.y = (busy.y + Math.random(3)) - 1
}
}