Click here to Skip to main content
15,891,375 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: EDITED Using template in C++ - run time error Pin
Victor Nijegorodov26-Nov-20 6:50
Victor Nijegorodov26-Nov-20 6:50 
AnswerRe: EDITED Using template in C++ - run time error Pin
jeron126-Nov-20 3:54
jeron126-Nov-20 3:54 
GeneralRe: EDITED Using template in C++ - run time error Pin
Member 1498043326-Nov-20 5:08
Member 1498043326-Nov-20 5:08 
GeneralRe: EDITED Using template in C++ - run time error Pin
jeron126-Nov-20 7:08
jeron126-Nov-20 7:08 
GeneralRe: EDITED Using template in C++ - run time error Pin
Member 1498043326-Nov-20 15:44
Member 1498043326-Nov-20 15:44 
GeneralRe: EDITED Using template in C++ - run time error Pin
jeron126-Nov-20 15:59
jeron126-Nov-20 15:59 
GeneralRe: EDITED Using template in C++ - run time error Pin
Richard MacCutchan26-Nov-20 21:26
mveRichard MacCutchan26-Nov-20 21:26 
QuestionGomoku in C++ Pin
Member 1499964622-Nov-20 2:39
Member 1499964622-Nov-20 2:39 
Hi . Recently i've got to make a game called gomoku between two players . Now i need to edit it and change it from PVP to versus computer . I understand that in order of getting a random number from computer i can use <time.h> and rand() , and the letter by something like this :
char letters[] = "abcdefghijklmnopqrstuvwxyz";
char x = letters[rand() % 26];

Now my question is how i can add it in my program , i tried to put them in but it still is not generating a move from computer . Here's my code,maybe u have some ideas
#include <iostream>
#include <iomanip>
using namespace std;

void print_table(int x[][15]) {
    system("cls");
	for (int i = 0; i < 15; i++) {//the loop that use to print out the english character row
		if (i == 0)
			cout << setw(4) << "A";
		else if (i == 1)
			cout << " B";
		else if (i == 2)
			cout << " C";
		else if (i == 3)
			cout << " D";
		else if (i == 4)
			cout << " E";
		else if (i == 5)
			cout << " F";
		else if (i == 6)
			cout << " G";
		else if (i == 7)
			cout << " H";
		else if (i == 8)
			cout << " I";
		else if (i == 9)
			cout << " J";
		else if (i == 10)
			cout << " K";
		else if (i == 11)
			cout << " L";
		else if (i == 12)
			cout << " M";
        else if (i == 13)
            cout << " N";
        else if (i == 14)
            cout << " O";
        else if (i == 15)
            cout << " P";
	}
	cout << endl;
	for (int i = 0; i < 15; i++) {
		cout << setw(2) << i;//print out the row number
		for (int j = 0; j < 15; j++) {//print out the board game.
			if (x[i][j] == 0) {//the inital value is 0, so when the block is 0 then print out the '.'
				cout << " .";
			}
			else if (x[i][j] == 1) {//when the player O input the block then the value will adding one then if check the block is one then output the 'o'
				cout << " O";
			}
			else if (x[i][j] == 2) {//when the player X input the block then the value will adding two then if check the block is two then output the 'x'
				cout << " X";
			}
		}
		cout << endl;
	}
}
int check_player(int p) {
	if (p == 1) {//change the player everytime before the next loop compile
		p++;
	}
	else {
		p--;
	}
	return p;
}
void input_value(int &t, int &n, int p, int x[][15]) {
	char eng;
	int number;
	do {//the loop that ask for the user input the location.
		cout << "player ";
		if (p == 1) {
			cout << "O";
		}
		else {
			cout << "X";
		}
		cout << ", make a move: ";
		cin >> eng;//input the location
		cin >> number;
		if (eng == 'A')//change the character to different number
			t = 0;
		else if (eng == 'B')
			t = 1;
		else if (eng == 'C')
			t = 2;
		else if (eng == 'D')
			t = 3;
		else if (eng == 'E')
			t = 4;
		else if (eng == 'F')
			t = 5;
		else if (eng == 'G')
			t = 6;
		else if (eng == 'H')
			t = 7;
		else if (eng == 'I')
			t = 8;
		else if (eng == 'J')
			t = 9;
		else if (eng == 'K')
			t = 10;
		else if (eng == 'L')
			t = 11;
		else if (eng == 'M')
			t = 12;
        else if (eng == 'N')
            t = 13;
        else if (eng == 'O')
            t = 14;
		if (!(eng >= 'A'&&eng <= 'M') || !(number >= 0 && number <= 14) || x[number][t] != 0) {//when the input wrong, output the statement to ask anouther input and loop again.
			cout << "Invaid input, Try again!" << endl;
			continue;
		}
		else {//if no problem then this input loop is break and jump to the next statement
			break;
		}
	} while (1);//Because it will break as well so the do-while loop is no any requirement
	n = number;
}
int main() {
	const int num = 15;//the number for constant the array row and column value
	char check_e;//for the user input the column
	int R[num][num] = { 0 }, check_n, player = 1, buger = 0, transfer, playerO_win = 0, playerX_win = 0, draw = 0, check_draw;//the variable that for user input or checking the game statment
	do {//launch the loop for the user input again and again
		check_draw = 0;//reset the checking of draw
		print_table(R);
		input_value(transfer, check_n, player, R);
		R[check_n][transfer] += player;//change the value according the player's input and the player name.
		for (int i = 0; i < num; i++) {
			for (int j = 0; j < num; j++) {
				if (i <= 8 && R[j][i] != 0 && (R[j][i] == R[j][i + 1] && R[j][i] == R[j][i + +2] && R[j][i] == R[j][i + 3] && R[j][i] == R[j][i + 4])) {//the checking for the row bingo
					if (R[j][i] == 1) {
						playerO_win++;
						break;
					}
					else {
						playerX_win++;
						break;
					}
				}
				else if (j <= 8 && R[j][i] != 0 && (R[j][i] == R[j + 1][i] && R[j][i] == R[j + 2][i] && R[j][i] == R[j + 3][i] && R[j][i] == R[j + 4][i])) {//the checking for the column bingo
					if (R[j][i] == 1) {
						playerO_win++;
						break;
					}
					else {
						playerX_win++;
						break;
					}
				}
				else if (j <= 8 && i <= 8 && R[j][i] != 0 && (R[j][i] == R[j + 1][i + 1] && R[j][i] == R[j + 2][i + 2] && R[j][i] == R[j + 3][i + 3] && R[j][i] == R[j + 4][i + 4])) {//the checking for the \ situation.
					if (R[j][i] == 1) {
						playerO_win++;
						break;
					}
					else {
						playerX_win++;
						break;
					}
				}
				else if ((j >= 4 || i >= 4 || i <= 8) && R[j][i] != 0 && (R[j][i] == R[j - 1][i + 1] && R[j][i] == R[j - 2][i + 2] && R[j][i] == R[j - 3][i + 3] && R[j][i] == R[j - 4][i + 4])) {//the checking for the / situation
					if (R[j][i] == 1) {
						playerO_win++;
						break;
					}
					else {
						playerX_win++;
						break;
					}
				}
				for (int i = 0; i < num; i++) {//the loop for checking the draw
					for (int j = 0; j < num; j++) {//this loop will check for every time compilation.
						if (R[j][i] == 0)//when there are any empty block then the check_draw will adding, the draw situation is the check_draw be 0
							check_draw++;
					}
				}
				if (check_draw == 0) {//when the check_draw equal to 0 which mean the situation is no empty block
					draw++;
					break;
				}

			}
			if (playerO_win != 0 || playerX_win != 0 || draw == 1)//break the second loop
				break;
		}
		if (playerO_win == 1 && playerX_win == 0) {// when the player win print the block game again and print out the win statement
			print_table(R);
			cout << "player O wins!" << endl;
			break;
		}
		else if (playerX_win == 1 && playerO_win == 0) {//the other player win the game
			print_table(R);
			cout << "player X wins!" << endl;
			break;
		}
		else if (draw == 1) {//the draw block game print
			print_table(R);
			cout << "Draw game!" << endl;
			break;
		}
		player = check_player(player);

	} while (1);//in fact it is no need for the loop statement, because most of the situation will have a break statement for out of the loop
	return 0;
}


modified 22-Nov-20 9:41am.

AnswerRe: Gomoku in C++ Pin
Greg Utas22-Nov-20 4:10
professionalGreg Utas22-Nov-20 4:10 
QuestionA "C" programming equivalent to Windows + R run dialog invokcation Pin
samtoad19-Nov-20 18:59
samtoad19-Nov-20 18:59 
AnswerRe: A "C" programming equivalent to Windows + R run dialog invokcation Pin
Randor 19-Nov-20 19:48
professional Randor 19-Nov-20 19:48 
GeneralRe: A "C" programming equivalent to Windows + R run dialog invokcation Pin
David Crow20-Nov-20 3:02
David Crow20-Nov-20 3:02 
PraiseRe: A "C" programming equivalent to Windows + R run dialog invokcation Pin
Randor 21-Nov-20 20:46
professional Randor 21-Nov-20 20:46 
QuestionRe: A "C" programming equivalent to Windows + R run dialog invokcation Pin
Richard MacCutchan20-Nov-20 4:18
mveRichard MacCutchan20-Nov-20 4:18 
AnswerRe: A "C" programming equivalent to Windows + R run dialog invokcation Pin
samtoad20-Nov-20 13:22
samtoad20-Nov-20 13:22 
QuestionRe: A "C" programming equivalent to Windows + R run dialog invokcation Pin
David Crow20-Nov-20 16:36
David Crow20-Nov-20 16:36 
GeneralRe: A "C" programming equivalent to Windows + R run dialog invokcation Pin
Dave Kreskowiak20-Nov-20 17:17
mveDave Kreskowiak20-Nov-20 17:17 
GeneralRe: A "C" programming equivalent to Windows + R run dialog invokcation Pin
Randor 21-Nov-20 21:06
professional Randor 21-Nov-20 21:06 
QuestionBeginner problem c++ Pin
begzz17-Nov-20 11:02
begzz17-Nov-20 11:02 
QuestionRe: Beginner problem c++ Pin
David Crow17-Nov-20 14:49
David Crow17-Nov-20 14:49 
SuggestionRe: Beginner problem c++ Pin
Richard MacCutchan17-Nov-20 21:11
mveRichard MacCutchan17-Nov-20 21:11 
QuestionSetting Up Visual Studio Code & MinGW Pin
Member 1499476816-Nov-20 17:06
Member 1499476816-Nov-20 17:06 
AnswerRe: Setting Up Visual Studio Code & MinGW Pin
Richard MacCutchan16-Nov-20 21:42
mveRichard MacCutchan16-Nov-20 21:42 
AnswerRe: Setting Up Visual Studio Code & MinGW Pin
Dave Kreskowiak17-Nov-20 8:37
mveDave Kreskowiak17-Nov-20 8:37 
QuestionPrime Number Code C Pin
iHuy14-Nov-20 22:06
iHuy14-Nov-20 22:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.