Skip to content

Instantly share code, notes, and snippets.

@uhfx uhfx/tic-tac-toe.ino
Last active Jan 14, 2019

Embed
What would you like to do?
2018 Logic Design
/**********************************************************/
/* LEDの点灯,消灯プログラムは以下のサイトを参考にした. */
/* http://tsukutta.hatenablog.com/entry/2013/05/30/150231 */
/**********************************************************/
const int rl[9] = { 22,26,32,36,42,46,52,56,60 }; // 赤色LEDのピンの定義
const int bl[9] = { 23,27,33,37,43,47,53,57,61 }; // 青色LEDのピンの定義
const int rs[9] = { 24,28,34,38,44,48,54,58,62 }; // 赤色スイッチのピンの定義
const int bs[9] = { 25,29,35,39,45,49,55,59,63 }; // 青色スイッチのピンの定義
// const int resetbutton = 64; // リセットボタンのピンの定義
const int win_red = 65; // 勝敗判定赤色のピンの定義
const int win_blue = 66; // 勝敗判定青色のピンの定義
/* 以下,ピンの間隔が狭かったのでボツ */
// const int rl[9] = { 22,26,30,34,38,42,46,50,54 }; // 赤色LEDのピンの定義
// const int bl[9] = { 23,27,31,35,39,43,47,51,55 }; // 青色LEDのピンの定義
// const int rs[9] = { 24,28,32,36,40,44,48,52,56 }; // 赤色スイッチのピンの定義
// const int bs[9] = { 25,29,33,37,41,45,49,53,57 }; // 青色スイッチのピンの定義
// const int resetbutton = 58; // リセットボタンのピンの定義
// const int win_red = 59; // 勝敗判定赤色のピンの定義
// const int win_blue = 60; // 勝敗判定青色のピンの定義
/* ボツ 終わり */
int r_value[9] = {}; // 入力ピンの状態がこの変数(val)に記憶される
int b_value[9] = {}; // 入力ピンの状態がこの変数(val)に記憶される
int r_old_value[9] = {}; // valの前の値を保存しておく変数
int b_old_value[9] = {}; // valの前の値を保存しておく変数
int r_state[9] = {}; // LEDの状態(0ならオフ、1ならオン)
int b_state[9] = {}; // LEDの状態(0ならオフ、1ならオン)
int reset = digitalRead(resetbutton);
void setup() {
pinMode(resetbutton, INPUT);
for (int i = 0; i < 9; i++) {
pinMode(rl[i], OUTPUT); // ArduinoにLEDが出力であると伝える
pinMode(bl[i], OUTPUT);
pinMode(rs[i], INPUT); // BUTTONは入力に設定
pinMode(bs[i], INPUT);
}
pinMode(win_red, OUTPUT);
pinMode(win_blue, OUTPUT);
}
void loop() {
/* リセットボタン(未実装) */
/* Arduino本体のリセットボタンで代用 */
if (reset == HIGH) {
for (int i = 0; i < 9; i++) {
r_state[i] = 0;
// r_old_value[i] = 0;
b_state[i] = 0;
// b_old_value[i] = 0;
}
}
/* スイッチの状態を読み込み */
for (int i = 0; i < 9; i++) {
r_value[i] = digitalRead(rs[i]);
b_value[i] = digitalRead(bs[i]);
}
/* LEDの点灯消灯判定 */
for (int i = 0; i < 9; i++) {
if ((r_value[i] == HIGH) && (r_old_value[i] == LOW)) {
r_state[i] = (r_state[i] + 1) % 2;
// delay(100);
}
r_old_value[i] = r_value[i]; // valはもう古くなったので保管しておく
}
for (int i = 0; i < 9; i++) {
if ((b_value[i] == HIGH) && (b_old_value[i] == LOW)) {
b_state[i] = (b_state[i] + 1) % 2;
// delay(100);
}
b_old_value[i] = b_value[i]; // valはもう古くなったので保管しておく
}
/* LEDの点灯消灯 */
for (int i = 0; i < 9; i++) {
if (r_state[i] == 0) {
digitalWrite(rl[i], LOW);
}
else if (r_state[i] == 1) {
digitalWrite(rl[i], HIGH);
}
}
for (int i = 0; i < 9; i++) {
if (b_state[i] == 0) {
digitalWrite(bl[i], LOW);
}
else if (b_state[i] == 1) {
digitalWrite(bl[i], HIGH);
}
}
/* 勝敗判定 */
if ((r_state[0] == 1) && (r_state[1] == 1) && (r_state[2] == 1)) {
digitalWrite(win_red, HIGH);
}
if ((b_state[0] == 1) && (b_state[1] == 1) && (b_state[2] == 1)) {
digitalWrite(win_blue, HIGH);
}
if ((r_state[3] == 1) && (r_state[4] == 1) && (r_state[5] == 1)) {
digitalWrite(win_red, HIGH);
}
if ((b_state[3] == 1) && (b_state[4] == 1) && (b_state[5] == 1)) {
digitalWrite(win_blue, HIGH);
}
if ((r_state[6] == 1) && (r_state[7] == 1) && (r_state[8] == 1)) {
digitalWrite(win_red, HIGH);
}
if ((b_state[6] == 1) && (b_state[7] == 1) && (b_state[8] == 1)) {
digitalWrite(win_blue, HIGH);
}
if ((r_state[0] == 1) && (r_state[3] == 1) && (r_state[6] == 1)) {
digitalWrite(win_red, HIGH);
}
if ((b_state[0] == 1) && (b_state[3] == 1) && (b_state[6] == 1)) {
digitalWrite(win_blue, HIGH);
}
if ((r_state[1] == 1) && (r_state[4] == 1) && (r_state[7] == 1)) {
digitalWrite(win_red, HIGH);
}
if ((b_state[1] == 1) && (b_state[4] == 1) && (b_state[7] == 1)) {
digitalWrite(win_blue, HIGH);
}
if ((r_state[2] == 1) && (r_state[5] == 1) && (r_state[8] == 1)) {
digitalWrite(win_red, HIGH);
}
if ((b_state[2] == 1) && (b_state[5] == 1) && (b_state[8] == 1)) {
digitalWrite(win_blue, HIGH);
}
if ((r_state[2] == 1) && (r_state[4] == 1) && (r_state[6] == 1)) {
digitalWrite(win_red, HIGH);
}
if ((b_state[2] == 1) && (b_state[4] == 1) && (b_state[6] == 1)) {
digitalWrite(win_blue, HIGH);
}
if ((r_state[0] == 1) && (r_state[4] == 1) && (r_state[8] == 1)) {
digitalWrite(win_red, HIGH);
}
if ((b_state[0] == 1) && (b_state[4] == 1) && (b_state[8] == 1)) {
digitalWrite(win_blue, HIGH);
}
else {
digitalWrite(win_red, LOW);
digitalWrite(win_blue, LOW);
}
/* 勝敗判定 終わり */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.