myRand, ugly, not yet working correctly
This commit is contained in:
parent
d361e11d50
commit
04a7077609
12
.cproject
12
.cproject
@ -25,14 +25,14 @@
|
||||
<option defaultValue="dk.xpg.msp430eclipse.compiler.option.optimization.level.none" id="dk.xpg.msp430eclipse.compiler.option.optimization.level.1547289165" name="Optimization Level" superClass="dk.xpg.msp430eclipse.compiler.option.optimization.level" valueType="enumerated"/>
|
||||
<option defaultValue="dk.xpg.msp430eclipse.compiler.option.debugging.level.default" id="dk.xpg.msp430eclipse.compiler.option.debugging.level.961192910" name="Debugging Level" superClass="dk.xpg.msp430eclipse.compiler.option.debugging.level" valueType="enumerated"/>
|
||||
<option id="dk.xpg.msp430eclipse.compiler.option.language.standard.1330784157" name="Standard" superClass="dk.xpg.msp430eclipse.compiler.option.language.standard" useByScannerDiscovery="false" value="dk.xpg.msp430eclipse.compiler.option.language.standard.gnu99" valueType="enumerated"/>
|
||||
<option id="dk.xpg.msp430eclipse.compiler.option.includes.paths.1317814193" superClass="dk.xpg.msp430eclipse.compiler.option.includes.paths" valueType="includePath">
|
||||
<option id="dk.xpg.msp430eclipse.compiler.option.includes.paths.1317814193" name="Include paths (-I)" superClass="dk.xpg.msp430eclipse.compiler.option.includes.paths" valueType="includePath">
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/TicTacToe/hottislib}""/>
|
||||
</option>
|
||||
<inputType id="dk.xpg.msp430eclipse.tool.compiler.gcc.input.1155150193" name="C Source File" superClass="dk.xpg.msp430eclipse.tool.compiler.gcc.input"/>
|
||||
<inputType id="dk.xpg.msp430eclipse.tool.compiler.gcc.input.cc.1230949175" name="C++ Source File" superClass="dk.xpg.msp430eclipse.tool.compiler.gcc.input.cc"/>
|
||||
</tool>
|
||||
<tool id="dk.xpg.msp430eclipse.tool.assembler.gcc.974609093" name="MSP430 Assembler" superClass="dk.xpg.msp430eclipse.tool.assembler.gcc">
|
||||
<option id="dk.xpg.msp430eclipse.assembler.option.general.include.483037451" superClass="dk.xpg.msp430eclipse.assembler.option.general.include" valueType="includePath">
|
||||
<option id="dk.xpg.msp430eclipse.assembler.option.general.include.483037451" name="Include Paths (-I)" superClass="dk.xpg.msp430eclipse.assembler.option.general.include" valueType="includePath">
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/TicTacToe/hottislib}""/>
|
||||
</option>
|
||||
<inputType id="dk.xpg.msp430eclipse.tool.assembler.input.669783994" name="Assembler Files" superClass="dk.xpg.msp430eclipse.tool.assembler.input"/>
|
||||
@ -44,7 +44,7 @@
|
||||
</toolChain>
|
||||
</folderInfo>
|
||||
<sourceEntries>
|
||||
<entry excluding="hottislib|src" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
|
||||
<entry excluding="myRand.h|hottislib|src" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="hottislib"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="src"/>
|
||||
</sourceEntries>
|
||||
@ -75,14 +75,14 @@
|
||||
<tool id="dk.xpg.msp430eclipse.tool.compiler.gcc.822429366" name="MSP430 C Compiler" superClass="dk.xpg.msp430eclipse.tool.compiler.gcc">
|
||||
<option defaultValue="dk.xpg.msp430eclipse.compiler.option.optimization.level.most" id="dk.xpg.msp430eclipse.compiler.option.optimization.level.135723235" name="Optimization Level" superClass="dk.xpg.msp430eclipse.compiler.option.optimization.level" valueType="enumerated"/>
|
||||
<option defaultValue="dk.xpg.msp430eclipse.compiler.option.debugging.level.none" id="dk.xpg.msp430eclipse.compiler.option.debugging.level.1183049546" name="Debugging Level" superClass="dk.xpg.msp430eclipse.compiler.option.debugging.level" valueType="enumerated"/>
|
||||
<option id="dk.xpg.msp430eclipse.compiler.option.includes.paths.247408562" superClass="dk.xpg.msp430eclipse.compiler.option.includes.paths" valueType="includePath">
|
||||
<option id="dk.xpg.msp430eclipse.compiler.option.includes.paths.247408562" name="Include paths (-I)" superClass="dk.xpg.msp430eclipse.compiler.option.includes.paths" valueType="includePath">
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/TicTacToe/hottislib}""/>
|
||||
</option>
|
||||
<inputType id="dk.xpg.msp430eclipse.tool.compiler.gcc.input.743092204" name="C Source File" superClass="dk.xpg.msp430eclipse.tool.compiler.gcc.input"/>
|
||||
<inputType id="dk.xpg.msp430eclipse.tool.compiler.gcc.input.cc.534769254" name="C++ Source File" superClass="dk.xpg.msp430eclipse.tool.compiler.gcc.input.cc"/>
|
||||
</tool>
|
||||
<tool id="dk.xpg.msp430eclipse.tool.assembler.gcc.1808009534" name="MSP430 Assembler" superClass="dk.xpg.msp430eclipse.tool.assembler.gcc">
|
||||
<option id="dk.xpg.msp430eclipse.assembler.option.general.include.1099305810" superClass="dk.xpg.msp430eclipse.assembler.option.general.include" valueType="includePath">
|
||||
<option id="dk.xpg.msp430eclipse.assembler.option.general.include.1099305810" name="Include Paths (-I)" superClass="dk.xpg.msp430eclipse.assembler.option.general.include" valueType="includePath">
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/TicTacToe/hottislib}""/>
|
||||
</option>
|
||||
<inputType id="dk.xpg.msp430eclipse.tool.assembler.input.490974509" name="Assembler Files" superClass="dk.xpg.msp430eclipse.tool.assembler.input"/>
|
||||
@ -94,7 +94,7 @@
|
||||
</toolChain>
|
||||
</folderInfo>
|
||||
<sourceEntries>
|
||||
<entry excluding="src" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
|
||||
<entry excluding="myRand.h|src" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="src"/>
|
||||
</sourceEntries>
|
||||
</configuration>
|
||||
|
6
.project
6
.project
@ -5,6 +5,11 @@
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.python.pydev.PyDevBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
|
||||
<triggers>clean,full,incremental,</triggers>
|
||||
@ -23,5 +28,6 @@
|
||||
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
|
||||
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
|
||||
<nature>dk.xpg.msp430eclipse.msp430nature</nature>
|
||||
<nature>org.python.pydev.pythonNature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
|
5
.pydevproject
Normal file
5
.pydevproject
Normal file
@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<?eclipse-pydev version="1.0"?><pydev_project>
|
||||
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
|
||||
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python interpreter</pydev_property>
|
||||
</pydev_project>
|
7
GenerateNumbers/genNum.py
Normal file
7
GenerateNumbers/genNum.py
Normal file
@ -0,0 +1,7 @@
|
||||
import random
|
||||
|
||||
t = ''
|
||||
for i in range(1000):
|
||||
t += '' + str(random.randint(0,8)) + ','
|
||||
|
||||
print t
|
169
src/game.c
169
src/game.c
@ -5,12 +5,19 @@
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "PontCoopScheduler.h"
|
||||
|
||||
#include "leds.h"
|
||||
#include "game.h"
|
||||
#include "myRand.h"
|
||||
|
||||
|
||||
|
||||
@ -19,47 +26,161 @@ tCellState field[FIELD_SIZE];
|
||||
|
||||
|
||||
|
||||
static void setDirectCell(uint8_t cell, tCellState state) {
|
||||
field[cell] = state;
|
||||
}
|
||||
|
||||
static void setCell(uint8_t row, uint8_t col, tCellState state) {
|
||||
field[(row * ROW_SIZE) + col] = state;
|
||||
}
|
||||
|
||||
void gameMove() {
|
||||
static uint8_t row = 0;
|
||||
static uint8_t col = 0;
|
||||
static tCellState getDirectCell(uint8_t cell) {
|
||||
return field[cell];
|
||||
}
|
||||
|
||||
static enum { sNEXT, sCROSS, sCIRCLE } state = sCROSS;
|
||||
static tCellState getCell(uint8_t row, uint8_t col) {
|
||||
return field[(row * ROW_SIZE) + col];
|
||||
}
|
||||
|
||||
static void setField(tCellState s) {
|
||||
for (uint8_t i = 0; i < FIELD_SIZE; i++) {
|
||||
field[i] = s;
|
||||
}
|
||||
}
|
||||
|
||||
static void play(tCellState state) {
|
||||
while (true) {
|
||||
uint8_t r = myRand();
|
||||
if (getDirectCell(r) == csEMPTY) {
|
||||
setDirectCell(r, state);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void playCircle() {
|
||||
play(csCIRCLE);
|
||||
}
|
||||
|
||||
static void playCross() {
|
||||
play(csCROSS);
|
||||
}
|
||||
|
||||
|
||||
typedef enum { arNOWINNER, arCROSS, arCIRCLE, arGAMEOVER } tArbiterResult;
|
||||
|
||||
static tArbiterResult mapCellStateToArbiterResult(tCellState c) {
|
||||
tArbiterResult a;
|
||||
if (c == csEMPTY) {
|
||||
a = arNOWINNER;
|
||||
} else if (c == csCROSS) {
|
||||
a = arCROSS;
|
||||
} else if (c == csCIRCLE) {
|
||||
a = arCIRCLE;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
static tArbiterResult arbiter() {
|
||||
tCellState baseState;
|
||||
|
||||
// checks rows
|
||||
for (uint8_t row = 0; row < ROW_SIZE; row++) {
|
||||
baseState = getCell(row, 0);
|
||||
if (baseState == csEMPTY) {
|
||||
continue;
|
||||
}
|
||||
if ((getCell(row, 1) == baseState) && getCell(row, 2) == baseState) {
|
||||
return mapCellStateToArbiterResult(baseState);
|
||||
}
|
||||
}
|
||||
|
||||
// checks cols
|
||||
for (uint8_t col = 0; col < COL_SIZE; col++) {
|
||||
baseState = getCell(0, col);
|
||||
if (baseState == csEMPTY) {
|
||||
continue;
|
||||
}
|
||||
if ((getCell(1, col) == baseState) && getCell(2, col) == baseState) {
|
||||
return mapCellStateToArbiterResult(baseState);
|
||||
}
|
||||
}
|
||||
|
||||
// checks diagonal 1
|
||||
baseState = getCell(0, 0);
|
||||
if ((baseState != csEMPTY) &&
|
||||
(getCell(1, 1) == baseState) &&
|
||||
(getCell(2, 2) == baseState)) {
|
||||
return mapCellStateToArbiterResult(baseState);
|
||||
}
|
||||
|
||||
// checks diagonal 2
|
||||
baseState = getCell(2, 0);
|
||||
if ((baseState != csEMPTY) &&
|
||||
(getCell(1, 1) == baseState) &&
|
||||
(getCell(0, 2) == baseState)) {
|
||||
return mapCellStateToArbiterResult(baseState);
|
||||
}
|
||||
|
||||
// check for free fields
|
||||
for (uint8_t i = 0; i < FIELD_SIZE; i++) {
|
||||
if (getDirectCell(i) == csEMPTY) {
|
||||
return mapCellStateToArbiterResult(csEMPTY);
|
||||
}
|
||||
}
|
||||
|
||||
return arGAMEOVER;
|
||||
}
|
||||
|
||||
|
||||
void gameMove() {
|
||||
static enum { sNEW, sCROSS, sCIRCLE, sARBITER1, sARBITER2 } state = sNEW;
|
||||
tArbiterResult winner;
|
||||
switch (state) {
|
||||
case sNEW:
|
||||
setField(csEMPTY);
|
||||
state = sCROSS;
|
||||
break;
|
||||
case sCROSS:
|
||||
setCell(row, col, csCROSS);
|
||||
state = sCIRCLE;
|
||||
playCross();
|
||||
state = sARBITER1;
|
||||
break;
|
||||
case sARBITER1:
|
||||
winner = arbiter();
|
||||
if (winner == arNOWINNER) {
|
||||
state = sCIRCLE;
|
||||
} else if (winner == arGAMEOVER) {
|
||||
setField(csEMPTY);
|
||||
state = sNEW;
|
||||
} else {
|
||||
setField(winner);
|
||||
state = sNEW;
|
||||
}
|
||||
break;
|
||||
case sCIRCLE:
|
||||
setCell(row, col, csCIRCLE);
|
||||
state = sNEXT;
|
||||
playCircle();
|
||||
state = sARBITER2;
|
||||
break;
|
||||
case sNEXT:
|
||||
setCell(row, col, csEMPTY);
|
||||
col++;
|
||||
if (col >= COL_SIZE) {
|
||||
col = 0;
|
||||
row++;
|
||||
if (row >= ROW_SIZE) {
|
||||
row = 0;
|
||||
}
|
||||
case sARBITER2:
|
||||
winner = arbiter();
|
||||
if (winner == arNOWINNER) {
|
||||
state = sCROSS;
|
||||
} else if (winner == arGAMEOVER) {
|
||||
setField(csEMPTY);
|
||||
state = sNEW;
|
||||
} else {
|
||||
setField(winner);
|
||||
state = sNEW;
|
||||
}
|
||||
state = sCROSS;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void gameInit() {
|
||||
for (uint8_t i = 0; i < FIELD_SIZE; i++) {
|
||||
field[i] = csEMPTY;
|
||||
}
|
||||
|
||||
schAdd(gameMove, NULL, 0, 1000);
|
||||
|
||||
setField(csEMPTY);
|
||||
schAdd(gameMove, NULL, 0, 100);
|
||||
}
|
||||
|
||||
tCellState* gameGetField() {
|
||||
|
23
src/myRand.c
Normal file
23
src/myRand.c
Normal file
@ -0,0 +1,23 @@
|
||||
/*
|
||||
* myRand.c
|
||||
*
|
||||
* Created on: Apr 29, 2018
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
uint8_t numbers[] = { 3, 5, 1, 3, 8, 4, 0, 4, 5, 3, 8, 2, 1, 7, 5, 2, 8, 7, 6, 5,
|
||||
2, 4, 3, 1, 1, 2, 4, 6, 7, 0, 8, 0, 3, 1, 6, 8, 8, 5, 3, 0, 2, 2, 6, 3,
|
||||
5, 5, 7, 3, 6, 2, 3, 3, 8, 0, 5, 3, 7, 8, 7, 2, 5, 2, 3, 5, 8, 6, 0, 2,
|
||||
};
|
||||
|
||||
uint8_t myRand() {
|
||||
static uint32_t i = 0;
|
||||
uint8_t r = numbers[i];
|
||||
i++;
|
||||
if (i > sizeof(numbers)) {
|
||||
i = 0;
|
||||
}
|
||||
return r;
|
||||
}
|
16
src/myRand.h
Normal file
16
src/myRand.h
Normal file
@ -0,0 +1,16 @@
|
||||
/*
|
||||
* myRand.h
|
||||
*
|
||||
* Created on: Apr 29, 2018
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
#ifndef MYRAND_H_
|
||||
#define MYRAND_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
uint8_t myRand();
|
||||
|
||||
|
||||
#endif /* MYRAND_H_ */
|
Loading…
x
Reference in New Issue
Block a user