File indexing completed on 2024-11-10 03:26:53
0001 /* 0002 SPDX-FileCopyrightText: 2005-2006 Albert Astals Cid <aacid@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "blinkengame.h" 0008 0009 #include <QRandomGenerator> 0010 #include <QTimer> 0011 0012 #include "soundsplayer.h" 0013 0014 blinkenGame::blinkenGame() : m_phase(starting) 0015 { 0016 m_soundsPlayer = new soundsPlayer; 0017 m_waitTimer = new QTimer(this); 0018 connect(m_waitTimer, &QTimer::timeout, this, &blinkenGame::waiting); 0019 } 0020 0021 blinkenGame::~blinkenGame() 0022 { 0023 delete m_soundsPlayer; 0024 } 0025 0026 int blinkenGame::level() const 0027 { 0028 return m_level; 0029 } 0030 0031 bool blinkenGame::canType() const 0032 { 0033 return m_phase == typingTheSequence || m_phase == starting; 0034 } 0035 0036 blinkenGame::gamePhase blinkenGame::phase() const 0037 { 0038 return m_phase; 0039 } 0040 0041 int blinkenGame::score() const 0042 { 0043 if (m_phase == starting || m_phase == choosingLevel) return 0; 0044 return m_sequenceLength - 1; 0045 } 0046 0047 void blinkenGame::clicked(color c) 0048 { 0049 if (m_phase == starting) 0050 { 0051 m_soundsPlayer -> play(c); 0052 return; 0053 } 0054 if (c == *m_nextColor) 0055 { 0056 ++m_nextColor; 0057 m_soundsPlayer -> play(c); 0058 0059 if (m_nextColor == m_sequence.constEnd()) 0060 { 0061 m_sequenceLength++; 0062 nextRound(); 0063 } 0064 } 0065 else 0066 { 0067 m_soundsPlayer -> play(all); 0068 Q_EMIT highlight(all, true); 0069 Q_EMIT gameEnded(); 0070 setPhase(choosingLevel); 0071 } 0072 } 0073 0074 void blinkenGame::setPhase(gamePhase p) 0075 { 0076 if (p != waiting3 && p != waiting2 && p != waiting1) m_waitTimer -> stop(); 0077 m_phase = p; 0078 Q_EMIT phaseChanged(); 0079 } 0080 0081 void blinkenGame::start(int level) 0082 { 0083 m_level = level; 0084 m_sequenceLength = 1; 0085 0086 nextRound(); 0087 0088 m_sequence.clear(); 0089 } 0090 0091 void blinkenGame::nextSound() 0092 { 0093 if (m_nextColor != m_sequence.constEnd()) 0094 { 0095 color c; 0096 c = *m_nextColor; 0097 ++m_nextColor; 0098 m_soundsPlayer -> play(c); 0099 Q_EMIT highlight(c, false); 0100 } 0101 else 0102 { 0103 setPhase(typingTheSequence); 0104 m_nextColor = m_sequence.constBegin(); 0105 Q_EMIT highlight(none, false); 0106 m_soundsPlayer->disconnect(); 0107 } 0108 } 0109 0110 void blinkenGame::soundEnded() 0111 { 0112 QTimer::singleShot(100, this, &blinkenGame::nextSound); 0113 QTimer::singleShot(50, this, &blinkenGame::unhighlight); 0114 } 0115 0116 void blinkenGame::unhighlight() 0117 { 0118 Q_EMIT highlight(none, false); 0119 } 0120 0121 void blinkenGame::waiting() 0122 { 0123 if (m_phase == waiting1) 0124 { 0125 setPhase(blinkenGame::learningTheSequence); 0126 if (m_level == 3) 0127 { 0128 m_sequence.clear(); 0129 for (int i = 0; i < m_sequenceLength; i++) m_sequence.append(generateColor()); 0130 } 0131 else m_sequence.append(generateColor()); 0132 0133 connect(m_soundsPlayer, &soundsPlayer::ended, this, &blinkenGame::soundEnded); 0134 m_nextColor = m_sequence.constBegin(); 0135 soundEnded(); 0136 } 0137 else if (m_phase == waiting3) setPhase(waiting2); 0138 else /* m_phase == waiting2 */ setPhase(waiting1); 0139 } 0140 0141 void blinkenGame::nextRound() 0142 { 0143 if (m_level == 1) setPhase(waiting3); 0144 else setPhase(waiting2); 0145 m_waitTimer -> start(1000); 0146 } 0147 0148 blinkenGame::color blinkenGame::generateColor() 0149 { 0150 // make the compiler happy :-D 0151 color c = none; 0152 0153 const int r = QRandomGenerator::global()->bounded(1, 5); // rand [1, 5) 0154 switch(r) 0155 { 0156 case 1: 0157 c = red; 0158 break; 0159 0160 case 2: 0161 c = green; 0162 break; 0163 0164 case 3: 0165 c = blue; 0166 break; 0167 0168 case 4: 0169 c = yellow; 0170 break; 0171 } 0172 return c; 0173 } 0174 0175 #include "moc_blinkengame.cpp"