0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

テトロミノ認識

Last updated at Posted at 2015-03-01
Source.cpp
#include "Header.h"

Tetromino::Tetromino()
{
	m_allDirections = 0;
	m_diagonally = 0;
	m_fourMass = 0;
	m_keimatobi = 0;
	m_same = 0;
}

int Tetromino::countAllDirectionsBlacks(const int& diff)
{
	switch(diff){
		case 1:
		case 10: return 1;
		default: return 0;
	}
}

int Tetromino::countDiagonallyBlacks(const int& diff)
{
	switch(diff){
		case 9:
		case 11: return 1;
		default: return 0;
	}
}

int Tetromino::countFourMassBlacks(const int& diff)
{
	switch(diff){
		case 3:
		case 30: return 1;
		default: return 0;
	}
}

int Tetromino::countKeimatobiBlacks(const int& diff)
{
	switch(diff){
		case 8:
		case 12:
		case 19:
		case 21: return 1;
		default: return 0;
	}
}

int Tetromino::countSameBlacks(const int& diff)
{
	if(diff == 0){
		return 1;
	}
	return 0;
}

bool Tetromino::isTetromino(const int& allDirections, const int& diagonally, const int& fourMass, const int& keimatobi)
{
	if(m_allDirections != allDirections)
		return false;
	if(m_diagonally != diagonally)
		return false;
	if(m_fourMass != fourMass)
		return false;
	if(m_keimatobi != keimatobi)
		return false;
	return true;
}

const std::string Tetromino::checkTetrominos()
{
	if(BLOCKDATA < m_same)
		return "-";
	if(isTetromino(6, 2, 0, 2))
		return "L";
	if(isTetromino(6, 0, 2, 0))
		return "I";
	if(isTetromino(6, 4, 0, 0))
		return "T";
	if(isTetromino(8, 4, 0, 0))
		return "O";
	if(isTetromino(6, 4, 0, 2))
		return "S";
	return "-";
}

void Tetromino::setTetrominos(const std::string& input)
{
	for(int index=0; index<BLOCKDATA; index++){
		std::string tetoriMino = input.substr(index*3, 2);
		m_filds[index] = (int)(tetoriMino.at(0) - '0') * 10 + (int)(tetoriMino.at(1) - '0');
	}
}

const std::string Tetromino::solve(const std::string& input)
{
	setTetrominos(input);
 	for(int i=0; i<BLOCKDATA; i++){
		for(int j=0; j<BLOCKDATA; j++){
			int diff = abs(m_filds[i] - m_filds[j]);
			m_allDirections += countAllDirectionsBlacks(diff);
			m_diagonally += countDiagonallyBlacks(diff);
			m_fourMass += countFourMassBlacks(diff);
			m_keimatobi += countKeimatobiBlacks(diff);
			m_same += countSameBlacks(diff);
		}
	}

	std::string result = checkTetrominos();
	return result;
}

void test(const std::string& input, const std::string& output)
{
	Tetromino tm;
	const std::string result = tm.solve(input);
	if(result == output){
		std::cout << "test is OK" << std::endl;
	}else{
		std::cout << "test is NG" << std::endl;
	}
}

int main()
{
  /*#1*/ test("55,55,55,55", "-");
  /*#2*/ test("39,28,27,29", "L");
  /*#3*/ test("63,62,43,53", "L");
  /*#4*/ test("32,42,43,44", "L");
  /*#5*/ test("81,72,91,71", "L");
  /*#6*/ test("62,64,72,63", "L");
  /*#7*/ test("45,25,35,24", "L");
  /*#8*/ test("12,20,22,21", "L");
  /*#9*/ test("66,46,67,56", "L");
  /*#10*/ test("44,46,45,43", "I");
  /*#11*/ test("04,24,14,34", "I");
  /*#12*/ test("43,42,41,40", "I");
  /*#13*/ test("48,38,58,68", "I");
  /*#14*/ test("31,20,22,21", "T");
  /*#15*/ test("69,79,78,89", "T");
  /*#16*/ test("42,33,44,43", "T");
  /*#17*/ test("16,25,05,15", "T");
  /*#18*/ test("27,37,28,38", "O");
  /*#19*/ test("13,24,23,14", "O");
  /*#20*/ test("63,72,62,73", "O");
  /*#21*/ test("73,63,62,74", "S");
  /*#22*/ test("56,57,47,66", "S");
  /*#23*/ test("88,99,98,87", "S");
  /*#24*/ test("62,43,52,53", "S");
  /*#25*/ test("86,95,87,96", "S");
  /*#26*/ test("84,83,73,94", "S");
  /*#27*/ test("32,33,41,42", "S");
  /*#28*/ test("86,85,75,96", "S");
  /*#29*/ test("97,76,96,77", "-");
  /*#30*/ test("53,55,45,43", "-");
  /*#31*/ test("73,93,94,84", "-");
  /*#32*/ test("31,33,41,42", "-");
  /*#33*/ test("21,32,11,12", "-");
  /*#34*/ test("73,75,65,64", "-");
  /*#35*/ test("64,65,45,54", "-");
  /*#36*/ test("12,00,01,10", "-");
  /*#37*/ test("94,85,75,74", "-");
  /*#38*/ test("87,86,77,75", "-");
  /*#39*/ test("56,56,56,56", "-");
  /*#40*/ test("41,42,41,52", "-");
  /*#41*/ test("61,60,63,61", "-");
  /*#42*/ test("03,13,33,13", "-");
  /*#43*/ test("92,96,94,93", "-");
  /*#44*/ test("15,25,55,45", "-");
  /*#45*/ test("17,14,16,13", "-");
  /*#46*/ test("72,83,83,92", "-");
  /*#47*/ test("40,40,42,51", "-");
  /*#48*/ test("81,80,93,82", "-");
  /*#49*/ test("51,61,30,41", "-");
  /*#50*/ test("17,37,35,15", "-");
	return 0;
}

Header.h
#ifndef _HEADER_H
#define _HEADER_H

#include <string>
#include <iostream>
#include <math.h>

#define BLOCKDATA 4
#define FILDSIZE 10

void test(const std::string& input, const std::string& output);

class Tetromino
{
public:
	Tetromino();
	const std::string solve(const std::string& input);

private:
	void setTetrominos(const std::string& input);
	const std::string checkTetrominos();
	int countAllDirectionsBlacks(const int& diff);
	int countDiagonallyBlacks(const int& diff);
	int countFourMassBlacks(const int& diff);
	int countKeimatobiBlacks(const int& diff);
	int countSameBlacks(const int& diff);
	bool isTetromino(const int& allDirections, const int& diagonally, const int& fourMass, const int& keimatobi);

	int m_filds[BLOCKDATA];
	int m_allDirections;
	int m_diagonally;
	int m_fourMass;
	int m_keimatobi;
	int m_same;
};

#endif
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?