You can implement the tic-tac-toe AI as a tree. One of the neat things about this is that you can do very few look-aheads and still be able to find every possible gameplay by pivoting around sets of nodes.
Forum
Off Topic C++ TutorialYou can implement the tic-tac-toe AI as a tree. One of the neat things about this is that you can do very few look-aheads and still be able to find every possible gameplay by pivoting around sets of nodes.
http://www.filefactory.com/file/b3dgg4f/n/sfmltest03.7z
EDit
Is there a better way to do this?
More
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using namespace std; namespace GUI { 	 	class Object; 	vector<GUI::Object*> Objects; 	 	class Object 	{ 	public: 		 		Object() 		{ 			GUI::Objects.push_back(this); 		} 		 		~Object() 		{ 			for ( int i = 0; i < GUI::Objects.size(); i++ ) 			{ 				if ( GUI::Objects[i] == this ) 				{ 					GUI::Objects.erase(GUI::Objects.begin()+i); 					break; 				} 			} 		} 		 		bool isActive; 		float X,Y,WX,WY; 		 		sf::Clock TMRTextInput; 		 		virtual void MouseHover(float _x, float _y) 		{ 			// Do nothing 		} 		 		virtual void MouseClicked(int MB) 		{ 			// Do nothing 		} 		 		virtual void GainActive() 		{ 			// Do nothing 		} 		 		virtual void LoseActive() 		{ 			// Do nothing 		} 		 		virtual void CharacterInput(const char* CHAR, int I) 		{ 			cout << CHAR << I; 		} 		 		virtual int Update(sf::RenderWindow* RENW) 		{ 			sf::Vector2f MousePos = RENW->ConvertCoords(RENW->GetInput().GetMouseX(), RENW->GetInput().GetMouseY()); 			if ( MousePos.x >= this->X && MousePos.y >= this->Y 				&& MousePos.x <= (this->X + this->WX) && MousePos.y <= (this->Y + this->WY)) 			{ 				this->MouseHover(MousePos.x - this->X, MousePos.y - this->Y); 				 				if ( RENW->GetInput().IsMouseButtonDown(sf::Mouse::Right) ) 					this->MouseClicked(0); 				if ( RENW->GetInput().IsMouseButtonDown(sf::Mouse::Middle) ) 					this->MouseClicked(1); 				if ( RENW->GetInput().IsMouseButtonDown(sf::Mouse::Left) ) 				{ 					this->MouseClicked(2); 					for ( int i = 0; i<GUI::Objects.size(); i++ ) 					{ 						if ( GUI::Objects[i] != this ) 						{ 							GUI::Objects[i]->isActive = false; 							GUI::Objects[i]->LoseActive(); 						} 						else 						{ 							GUI::Objects[i]->isActive = true; 							GUI::Objects[i]->GainActive(); 						} 					} 				} 			} 			 			if ( this->isActive ) 			{ 				if ( TMRTextInput.GetElapsedTime() >= 0.2f ) 				{ 					if ( RENW->GetInput().IsKeyDown(sf::Key::LShift) ^ RENW->GetInput().IsKeyDown(sf::Key::RShift) ) 					{ 						if ( RENW->GetInput().IsKeyDown(sf::Key::A) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::A; I_ = (int) K_; this->CharacterInput("a", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::B) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::B; I_ = (int) K_; this->CharacterInput("b", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::C) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::C; I_ = (int) K_; this->CharacterInput("c", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::D) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::D; I_ = (int) K_; this->CharacterInput("d", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::E) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::E; I_ = (int) K_; this->CharacterInput("e", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::F) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::F; I_ = (int) K_; this->CharacterInput("f", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::G) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::G; I_ = (int) K_; this->CharacterInput("g", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::H) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::H; I_ = (int) K_; this->CharacterInput("h", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::I) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::I; I_ = (int) K_; this->CharacterInput("i", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::J) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::J; I_ = (int) K_; this->CharacterInput("j", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::K) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::K; I_ = (int) K_; this->CharacterInput("k", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::L) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::L; I_ = (int) K_; this->CharacterInput("l", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::M) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::M; I_ = (int) K_; this->CharacterInput("m", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::N) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::N; I_ = (int) K_; this->CharacterInput("n", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::O) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::O; I_ = (int) K_; this->CharacterInput("o", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::P) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::P; I_ = (int) K_; this->CharacterInput("p", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Q) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Q; I_ = (int) K_; this->CharacterInput("q", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::R) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::R; I_ = (int) K_; this->CharacterInput("r", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::S) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::S; I_ = (int) K_; this->CharacterInput("s", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::T) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::T; I_ = (int) K_; this->CharacterInput("t", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::U) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::U; I_ = (int) K_; this->CharacterInput("u", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::V) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::V; I_ = (int) K_; this->CharacterInput("v", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::W) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::W; I_ = (int) K_; this->CharacterInput("w", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::X) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::X; I_ = (int) K_; this->CharacterInput("x", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Y) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Y; I_ = (int) K_; this->CharacterInput("y", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Z) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Z; I_ = (int) K_; this->CharacterInput("z", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Num0) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Num0; I_ = (int) K_; this->CharacterInput(")", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Num1) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Num1; I_ = (int) K_; this->CharacterInput("!", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Num2) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Num2; I_ = (int) K_; this->CharacterInput("@", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Num3) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Num3; I_ = (int) K_; this->CharacterInput("#", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Num4) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Num4; I_ = (int) K_; this->CharacterInput("$", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Num5) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Num5; I_ = (int) K_; this->CharacterInput("%", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Num6) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Num6; I_ = (int) K_; this->CharacterInput("^", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Num7) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Num7; I_ = (int) K_; this->CharacterInput("&", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Num8) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Num8; I_ = (int) K_; this->CharacterInput("*", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Num9) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Num9; I_ = (int) K_; this->CharacterInput("(", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::LBracket) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::LBracket; I_ = (int) K_; this->CharacterInput("{", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::RBracket) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::RBracket; I_ = (int) K_; this->CharacterInput("}", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::SemiColon) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::SemiColon; I_ = (int) K_; this->CharacterInput(":", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Comma) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Comma; I_ = (int) K_; this->CharacterInput("<", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Period) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Period; I_ = (int) K_; this->CharacterInput(">", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Quote) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Quote; I_ = (int) K_; this->CharacterInput("\"", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Slash) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Slash; I_ = (int) K_; this->CharacterInput("?", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::BackSlash) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::BackSlash; I_ = (int) K_; this->CharacterInput("|", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Tilde) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Tilde; I_ = (int) K_; this->CharacterInput("~", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Equal) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Equal; I_ = (int) K_; this->CharacterInput("+", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Dash) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Dash; I_ = (int) K_; this->CharacterInput("_", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Space) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Space; I_ = (int) K_; this->CharacterInput(" ", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Return) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Return; I_ = (int) K_; this->CharacterInput(">>", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Back) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Back; I_ = (int) K_; this->CharacterInput("<<", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Tab) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Tab; I_ = (int) K_; this->CharacterInput("--", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Numpad0) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Numpad0; I_ = (int) K_; this->CharacterInput("0", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Numpad1) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Numpad1; I_ = (int) K_; this->CharacterInput("1", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Numpad2) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Numpad2; I_ = (int) K_; this->CharacterInput("2", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Numpad3) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Numpad3; I_ = (int) K_; this->CharacterInput("3", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Numpad4) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Numpad4; I_ = (int) K_; this->CharacterInput("4", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Numpad5) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Numpad5; I_ = (int) K_; this->CharacterInput("5", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Numpad6) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Numpad6; I_ = (int) K_; this->CharacterInput("6", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Numpad7) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Numpad7; I_ = (int) K_; this->CharacterInput("7", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Numpad8) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Numpad8; I_ = (int) K_; this->CharacterInput("8", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Numpad9) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Numpad9; I_ = (int) K_; this->CharacterInput("9", I_);} 					} 					else 					{ 						if ( RENW->GetInput().IsKeyDown(sf::Key::A) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::A; I_ = (int) K_; this->CharacterInput("a", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::B) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::B; I_ = (int) K_; this->CharacterInput("b", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::C) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::C; I_ = (int) K_; this->CharacterInput("c", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::D) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::D; I_ = (int) K_; this->CharacterInput("d", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::E) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::E; I_ = (int) K_; this->CharacterInput("e", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::F) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::F; I_ = (int) K_; this->CharacterInput("f", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::G) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::G; I_ = (int) K_; this->CharacterInput("g", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::H) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::H; I_ = (int) K_; this->CharacterInput("h", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::I) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::I; I_ = (int) K_; this->CharacterInput("i", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::J) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::J; I_ = (int) K_; this->CharacterInput("j", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::K) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::K; I_ = (int) K_; this->CharacterInput("k", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::L) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::L; I_ = (int) K_; this->CharacterInput("l", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::M) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::M; I_ = (int) K_; this->CharacterInput("m", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::N) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::N; I_ = (int) K_; this->CharacterInput("n", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::O) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::O; I_ = (int) K_; this->CharacterInput("o", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::P) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::P; I_ = (int) K_; this->CharacterInput("p", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Q) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Q; I_ = (int) K_; this->CharacterInput("q", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::R) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::R; I_ = (int) K_; this->CharacterInput("r", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::S) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::S; I_ = (int) K_; this->CharacterInput("s", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::T) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::T; I_ = (int) K_; this->CharacterInput("t", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::U) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::U; I_ = (int) K_; this->CharacterInput("u", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::V) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::V; I_ = (int) K_; this->CharacterInput("v", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::W) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::W; I_ = (int) K_; this->CharacterInput("w", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::X) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::X; I_ = (int) K_; this->CharacterInput("x", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Y) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Y; I_ = (int) K_; this->CharacterInput("y", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Z) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Z; I_ = (int) K_; this->CharacterInput("z", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Num0) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Num0; I_ = (int) K_; this->CharacterInput("0", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Num1) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Num1; I_ = (int) K_; this->CharacterInput("1", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Num2) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Num2; I_ = (int) K_; this->CharacterInput("2", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Num3) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Num3; I_ = (int) K_; this->CharacterInput("3", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Num4) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Num4; I_ = (int) K_; this->CharacterInput("4", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Num5) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Num5; I_ = (int) K_; this->CharacterInput("5", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Num6) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Num6; I_ = (int) K_; this->CharacterInput("6", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Num7) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Num7; I_ = (int) K_; this->CharacterInput("7", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Num8) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Num8; I_ = (int) K_; this->CharacterInput("8", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Num9) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Num9; I_ = (int) K_; this->CharacterInput("9", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::LBracket) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::LBracket; I_ = (int) K_; this->CharacterInput("[", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::RBracket) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::RBracket; I_ = (int) K_; this->CharacterInput("]", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::SemiColon) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::SemiColon; I_ = (int) K_; this->CharacterInput(";", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Comma) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Comma; I_ = (int) K_; this->CharacterInput(",", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Period) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Period; I_ = (int) K_; this->CharacterInput(".", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Quote) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Quote; I_ = (int) K_; this->CharacterInput("\"", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Slash) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Slash; I_ = (int) K_; this->CharacterInput("/", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::BackSlash) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::BackSlash; I_ = (int) K_; this->CharacterInput("\\", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Tilde) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Tilde; I_ = (int) K_; this->CharacterInput("~", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Equal) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Equal; I_ = (int) K_; this->CharacterInput("=", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Dash) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Dash; I_ = (int) K_; this->CharacterInput("-", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Space) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Space; I_ = (int) K_; this->CharacterInput(" ", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Return) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Return; I_ = (int) K_; this->CharacterInput(">>", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Back) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Back; I_ = (int) K_; this->CharacterInput("<<", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Tab) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Tab; I_ = (int) K_; this->CharacterInput("++", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Numpad0) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Numpad0; I_ = (int) K_; this->CharacterInput("0", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Numpad1) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Numpad1; I_ = (int) K_; this->CharacterInput("1", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Numpad2) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Numpad2; I_ = (int) K_; this->CharacterInput("2", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Numpad3) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Numpad3; I_ = (int) K_; this->CharacterInput("3", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Numpad4) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Numpad4; I_ = (int) K_; this->CharacterInput("4", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Numpad5) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Numpad5; I_ = (int) K_; this->CharacterInput("5", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Numpad6) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Numpad6; I_ = (int) K_; this->CharacterInput("6", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Numpad7) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Numpad7; I_ = (int) K_; this->CharacterInput("7", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Numpad8) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Numpad8; I_ = (int) K_; this->CharacterInput("8", I_);} 						if ( RENW->GetInput().IsKeyDown(sf::Key::Numpad9) ) {TMRTextInput.Reset(); int I_; sf::Key::Code K_ = sf::Key::Numpad9; I_ = (int) K_; this->CharacterInput("9", I_);} 					} 				} 			} 			 		} 	} ; }
PS: I did not write all that I used Lua to do it for me. I am too lazy to hand write all that.
edited 5×, last 10.10.10 04:26:21 am
http://www.sfml-dev.org/documentation/1.6/structsf_1_1Event_1_1TextEvent.htm
Or this:
http://www.sfml-dev.org/documentation/1.6/structsf_1_1Event_1_1KeyEvent.htm
I don't know anything about openGL though.
But in allegro I use a std::string and concatenate every character when the user presses a key (first you have to make sure the user didn't press the Enter key, backspace, Escape, etc)
Notice that TTF fonts have different widths on every character so a simple multiplication won't work.
edited 1×, last 27.10.10 03:06:04 am
Command line:
g++ main.cpp -static-libstdc++ -static-libgcc
main.cpp
More
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using namespace std; #include "Include/include.h" int main() { 	while ( true ) 	{ 		switch(getInput()) 		{ 			case ARROW_UP: 				printf("UP\n"); 				break; 		} 	} 	 	return 0; }
include.h
More
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef INCLUDE_H #define INCLUDE_H #include <string> #include <fstream> #include <stdlib.h> #include <stdio.h> #include <iostream> #include <conio.h> #include "keys.cpp" #include "objects.cpp" #include "controls.cpp" #include "map.cpp" #endif
map.cpp
More
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef MAP_H #define MAP_H #define MAX_MAP_X 0x2710 #define MAX_MAP_Y 0x2710 int mapx,mapy; object MAP_DATA[MAX_MAP_X][MAX_MAP_Y]; bool LoadMap(const char* path) { 	ifstream map(path); 	if ( map.is_open() ) 	{ 		std::string line; 		 		getline(map, line); 		if ( !line.compare("%CAN_OPEN") == 0 ) 			return false; 		else 		{ 			int X=0, Y=0; 			bool createdPlayer = false;				 			while ( map.good() ) 			{ 				getline(map, line); 				for ( int i = 0; i < line.length(); i++ ) 				{ 					std::string currentPos = line.substr(i,0); 					if ( currentPos.compare("#") == 0 ) 						MAP_DATA[X][Y].type_id = ID_WALL; 					else if ( currentPos.compare("P") == 0 && (!createdPlayer) ) 					{ 						MAP_DATA[X][Y].type_id = ID_PLAYER; 						createdPlayer = true; 					} 					else 						MAP_DATA[X][Y].type_id = ID_FLOOR; 					 					X++; 				} 				Y++; 			} 		} 	}		 				 	else 		return false; } #endif
controls.cpp
More
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#ifndef CONTROLS_H #define CONTROLS_H #ifndef OBJECTS_H #include OBJECTS_H #endif void UpdateMovement(object* obj) { 	switch ( getInput() ) 	{ 		case ARROW_UP: 			obj->pY += 1; 			break; 		 		case ARROW_DOWN: 			obj->pY -= 1; 			break; 		 		case ARROW_RIGHT: 			obj->pX += 1; 			break; 		 		case ARROW_LEFT: 			obj->pX -= 1; 			break; 	} } #endif
objects.cpp
More
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef OBJECTS_H #define OBJECTS_H #define MAX_ITEMNAME_LENTH 26 #define ID_WALL 0x0 #define ID_OBSTICLE 0x1 #define ID_PROJECTILE 0x2 #define ID_AMMO 0x3 #define ID_PLAYER 0x4 #define ID_FLOOR 0x5 struct object { 	int type_id; 	int pX,pY; 	float health; 	float shield; } ; #endif
keys.cpp
More
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef KEYS_H #define KEYS_H enum { ARROW_UP = 328, ARROW_DOWN = 336, ARROW_LEFT = 331, ARROW_RIGHT = 333, KEY_ESC = 27 } ; int getInput () { 	int ch = getch(); 	if ( ch == 0 || ch == 224 ) 		ch += getch(); 	return ch; } #endif
edited 7×, last 07.11.10 06:06:14 am
I know this thread is a little bit old (133 days), but I have a question:
Do you have some sites where I could learn how to make a simple (and i mean SIMPLE SIMPLE) compiler in C/C++?
Or creating an .exe file that does something. (simple console output would be a good start)
EDIT: I don't want to compile C++ code, I want to create a compiler for my own (LITTLE) scripting language.
Scheme is one of the easiest to implement languages out there. To create a full fledged language, you will need to learn to use one of several DSL specific toolkits (lex comes to mind), how to implement, build, and traverse AST trees, and how to atomically represent computations with a bijection into the actual computation space. These are the primitives for building your own language. (By compiler I assume you mean an interpretor, as the output produced by a compiler is not executable.)
but I just wanted to know if there's a way to create .exe files with C/C++.....
And it's not that i want to start a big project, I'm just curious.